最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Check for bool in JavaScript - Stack Overflow

programmeradmin1浏览0评论

I've got the following jQuery (I've got it wrapped in the document ready function and all that, so please know I'm just showing you the inside of the function.

..
            var itemIsSold = $("#itemIsSold").val();
            alert(itemIsSold);
            if(!itemIsSold) {
               ...
    }

where itemIsSold is a hidden input field. I get the value False upper case F when it hits the alert but never enters my next if statement. I know this has to be something stupid simple.

I've got the following jQuery (I've got it wrapped in the document ready function and all that, so please know I'm just showing you the inside of the function.

..
            var itemIsSold = $("#itemIsSold").val();
            alert(itemIsSold);
            if(!itemIsSold) {
               ...
    }

where itemIsSold is a hidden input field. I get the value False upper case F when it hits the alert but never enters my next if statement. I know this has to be something stupid simple.

Share Improve this question edited Nov 20, 2009 at 20:39 bdukes 156k25 gold badges150 silver badges176 bronze badges asked Nov 20, 2009 at 20:28 PositiveGuyPositiveGuy 47.8k112 gold badges314 silver badges479 bronze badges 1
  • Is there any way to check it as an actual bool? I'm trying to grab a bool property from my asp code-behind and right now I am putting that value into an input field but there's got to be a better way to grab it from JS other than a hidden field? – PositiveGuy Commented Nov 20, 2009 at 20:32
Add a ment  | 

2 Answers 2

Reset to default 8

If the input's value contains the string "False", that will not translate into a false boolean value. You will need to actually check for itemIsSold == "False".

Since the value of the hidden input field is a string, !"False" will be evaluated to false. Note that any string other than a string with the length of 0 is treated as true. So you should rather pare the string value to another string value like "False":

if (itemIsSold == "False") {
    // …
}
发布评论

评论列表(0)

  1. 暂无评论