JQuery test value coming back from select
It has options with values true
and false
.
I have s开发者_JAVA百科ome jQuery here:
$("#myselect").change(function () {
var accepted = $(this)[0].value;
alert(accepted);
if(accepted == false) {
alert("Hi");
}
});
What I don't understand is why the Hi message is never shown.
The condition in the if
is always coming through false
.
Could someone please help me with this I'm fairly new to jQuery.
It's because the value=""
of an input
is a string:
if (accepted == 'false')
http://jsfiddle.net/nAteT/
The value of a select is a string, not a boolean. A non-empty string is evaluated as the true value so it will never be equal to the boolean false. You should do your comparison as a string.
$("#myselect").change(function () {
var accepted = $(this).val();
alert(accepted);
if (accepted == 'false') {
alert("Hi");
}
});
It seems like the value of your dropdown is 'false'
, which is a string, whereas false
is a boolean.
So change your check to if(accepted == 'false')
.
精彩评论