你能解释为什么if条件在没有eval函数的情况下不起作用:
var myBoolean= document.getElementById("someBoolean").value; //This is a 'false'
if(myBoolean)
{
alert(Your boolean is True); //This condition always getting executed even though myBoolean is false;
}
if(eval(myBoolean))
{
alert("You will never see this alert bcoz boolean is false");
}发布于 2011-12-02 07:04:33
在Javascript中,以下值被视为条件条件的false:
空字符串''
falsenull undefinedNaN 0
其他所有内容都被视为true。
'false'不是上面的任何一个,所以它是true。
发布于 2011-12-02 06:48:58
字符串'false'的计算结果为布尔型true
发布于 2011-12-02 06:49:46
这是因为它实际上不是一个布尔值,而是一个字符串'false'。当您将字符串转换为布尔值时,''为false,其他任何值都为true。
您可以检查它是否等于字符串'false' (或'true')。
var myBoolean = 'false'; // (string)
myBoolean = myBoolean !== 'false'; //false (boolean)https://stackoverflow.com/questions/8349416
复制相似问题