Javascript-If和or语句-IE未定义

Javascript - If and or statements - IE Undefined

本文关键字:-IE 未定义 语句 or Javascript-If      更新时间:2023-09-26

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

在chrome中,当我单击运行该脚本的按钮时,弹出的文本框是空的,所以我检查长度是否大于0….

在IE中,它将其填充为未定义,所以我想我可以检查值并说if!=未定义。。。

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...');
}

有人知道我做错了什么吗?

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

参见MDN上的undefined

未定义是一种数据类型,而不是字符串值。如果你想与字符串进行比较,请尝试使用以下方法:

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

以防万一有人想知道。。。这就是我最终解决问题的方式…

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);
            }
        }