开发者

JQuery test value coming back from select

开发者 https://www.devze.com 2023-04-12 11:43 出处:网络
It has options with values true and false. I have s开发者_JAVA百科ome jQuery here: $(\"#myselect\").change(function () {

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').

0

精彩评论

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

关注公众号