Javascript - If和or statements - IE Undefined

时间:2015-05-29 12:47:19

标签: javascript

我很困惑,为什么这不起作用......

在Chrome中单击运行此脚本的按钮时,弹出的文本框为空,因此我检查长度> 0 .....

在IE中,它将其填充为未定义,所以我想我可以检查该值并说出是否!=未定义...

if (strTemp.length > 0) {
    if (strTemp.value != "undefined") {
        printLabels(strCarrier, strTemp);
    } else {
        alert('You wont get labels until you tell us why...');
    }
}
else {
    alert('You wont get labels until you tell us why...');
}

有人对我做错了什么有任何想法吗?

4 个答案:

答案 0 :(得分:4)

如果值为undefined,则不是字符串。您应该检查if (strTemp.value != undefined)

请参阅MDN上的undefined

答案 1 :(得分:2)

Undefined是一种数据类型,而不是字符串值。如果要与字符串进行比较,请尝试使用:

if (typeof strTemp.value === "undefined") {
    alert("strTemp.val is undefined");
}

答案 2 :(得分:0)

if (strTemp && strTemp.value) {
    printLabels(strCarrier, strTemp);
} else {
    alert('You wont get labels until you tell us why...');
}

答案 3 :(得分:0)

只是想让任何人想知道......这就是我最终解决它的方式......

if (strTemp.length > 1) {
            if ((strTemp == "undefined") || (strTemp == "")){
                alert('You wont get labels until you tell us why...');
            }else{
                printLabels(strCarrier, strTemp);
            }
        }else{
            if ((strTemp == "undefined") || (strTemp == "")){
                alert('You wont get labels until you tell us why...');
            }else{
                printLabels(strCarrier, strTemp);
            }

        }