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);
精彩评论