JQuery fails to react on a change when selecting blank option
When selecting blank option (positioned at 0th location) jquery fails to launch change trigger.
Update: My bad, it actually works just fine. I was trying to execute code in a wrong place. Oops. As always jQuery开发者_如何转开发 rocks!!!
Here what I have that work perfectly fine when selecting non-blank options:
<select id="asdf">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
$("#asdf").change(function() {
var value = $(f).val();
$.post("_asdf.php", {value:value}, function(data) {
if(parseInt(data) == 0) {
alert('bad');
}
else {
alert('good');
}
});
return false;
});
I used this code for a test
HTML:
<select>
<option></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<span></span>
Javascript:
$("select").change(function() {
$("span").text($(this).val());
});
It works fine for me, even if I run it with really old 1.2 version. You can try it in any online javascript tool, like http://jsfiddle.net/ for example.
I had the exact same problem with a different cause. In my case, I was doing a post that reloaded the drop down and removed the .change() event. Putting the $('select').change(fooFunction) in the $.post solved the problem.
精彩评论