开发者

How to reset a selectbox with jquery using chrome?

I just want to clear the selection of a html selectbox. The following works fine within Safari and Firefox but does not in Google Chrome:

$('select#selectbox').val(null);

Any ideas?

Whole js here: h开发者_运维技巧ttp://jsfiddle.net/EJgdA/8/


.val(null) only seems to work in FF and Opera, so I don't think it's a valid use case.

You can unset everything by using selectedIndex as such:

$('#selectbox').prop('selectedIndex', -1);

Note that .prop() only works in jQuery 1.6+, for lower versions you must use .attr().


Interesting. Have you tried plain js?

document.getElementById('selectbox').selectedIndex = -1;


$('select#selectbox').val(null); returns the value attribute of the selected option, you don't need to add select in your selector.

this will do the trick for you..

 $("#selectbox").find("option:selected").removeAttr("selected"); 

you van use this line of code also if you have jQuery1.6

$('#selectbox').prop('selectedIndex', -1); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜