Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-If和或语句-IE未定义_Javascript - Fatal编程技术网

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

Javascript-If和或语句-IE未定义,javascript,Javascript,我很困惑为什么这不起作用 在chrome中,当我单击运行此脚本的按钮时,弹出的文本框为空,因此我检查长度>0 在IE中,它将其填充为未定义,因此我想我可以检查该值并说如果!=未定义的 if (strTemp.length > 0) { if (strTemp.value != "undefined") { printLabels(strCarrier, strTemp); } else { alert('You wont get labels

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

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

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

如果值是未定义的,那么它不是字符串。您应该检查
是否(strTemp.value!=未定义)


请参见MDN上的。

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


请参见MDN上的。

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

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

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

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

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

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

        }

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

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

        }


undefined与字符串undefined不同…undefined不应该是stringuse
if(strTemp.length>0&&strTemp.value!=undefined)
@maiman应该是另一种方式。首先检查undefined,然后检查length。我认为这无关紧要,但我相信你的话:)undefined和字符串undefined不一样……undefined不应该是stringuse
if(strTemp.length>0&&strTemp.value!=undefined)
@maiman应该是另一种方式。首先检查未定义,然后检查长度。我认为这无关紧要,但我会接受你的说法:)或者使用typeof和string进行比较。完美。。。谢谢@scimonester所以这似乎不起作用,因为现在有了这组代码,它不会让任何东西通过?
if((strTemp.length>0)&&(!(strTemp.value==undefined)){printLabels(strCarrier,strTemp);}否则{alert('在你告诉我们为什么之前你不会得到标签…);}
@KyleRickaby尝试将未定义的检查放在长度检查之前。或者使用typeof并与字符串进行比较。完美。。。谢谢@scimonester所以这似乎不起作用,因为现在有了这组代码,它不会让任何东西通过?
if((strTemp.length>0)&&(!(strTemp.value==undefined)){printLabels(strCarrier,strTemp);}否则{alert('在你告诉我们为什么之前你不会得到标签…);}
@KyleRickaby尝试将未定义的检查放在长度检查之前。虽然此代码片段可能会解决问题,但确实有助于提高文章质量。请记住,您将在将来为读者回答这个问题,而这些人可能不知道您的代码建议的原因。
if(strTemp&&strTemp.value){printLabels(strCarrier,strTemp);}else{alert('在您告诉我们为什么之前您不会得到标签…);}
虽然此代码片段可以解决这个问题,确实有助于提高你的文章质量。请记住,您将在将来为读者回答这个问题,而这些人可能不知道您的代码建议的原因。
if(strTemp&&strTemp.value){printLabels(strCarrier,strTemp);}else{alert('在告诉我们为什么之前您不会得到标签…');}
if((strTemp.length>0)&&(typeof strTemp.value==“undefined”){printLabels(strCarrier,strTemp);}否则{alert('You't won't get labels,直到你告诉我们为什么…');}
在chrome中工作,但在IE中不工作,当它被设置为undefined
时((strTemp.length>0)&&(typeof strTemp.value==“undefined”){printLabels(strCarrier,strTemp);}否则{alert('在你告诉我们为什么…'之前你不会得到标签);}
在chrome中工作,但在IE中不工作,当它被设置为未定义时