你能解释为什么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:00:26
尝试:
var myBoolean= document.getElementById("someBoolean").value; //This is a 'false'
if(myBoolean != "false")
{
alert(Your boolean is True); //This condition always getting executed even though myBoolean is false;
}就像其他人所说的那样,字符串不是布尔值,所以把它当做布尔值使用会给你一个逻辑错误。
https://stackoverflow.com/questions/8349416
复制相似问题