Jquery:将字符串转换为布尔值

时间:2013-07-24 18:16:02

标签: jquery hidden-field

我正在汇集一些隐藏字段中的以下值。

var hfEntityBool = $(this).find('input[id*="hfEntityBool"]').val();
if (hfEntityBool) {
    alert(hfEntityBool);
}

无论隐藏字段的值如何,警报仍会显示。有什么理由吗?

我尝试使用布尔(hfEntityBool)方法,以防隐藏字段仍为字符串,但没有任何更改。

感谢帮助

2 个答案:

答案 0 :(得分:5)

这取决于您在隐藏字段中期望的值。

我会做这样的事情:

var hfEntityBool = $(this).find('input[id*="hfEntityBool"]').val() == "true";
if (hfEntityBool) {
    alert(hfEntityBool);
}

答案 1 :(得分:0)

你有没有尝试过:

if(hfEntityBool.length){...} // (or hfEntityBool.length > 0)

控制台显示了什么:

console.log('nb char of hfEntityBool : '+hfEntityBool.length);//before if