首页 > 解决方案 > 为什么我不能在 javascript 中使用 && 因为不是空白而不是空格

问题描述

我想检查值不是空白或一个空格所以我写了一个代码

var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value != " " && OccLocation.value != "") {
  alert("not empty");
}
<input type="hidden" id="HdnOccLocation" name="HdnOccLocation" value="" style="position:absolute;height:20px;color:#000000;text-align:left;font-size:12px;font-style:normal;width:26px;background-color:#00cccc;left:800px;font-weight:normal;top:220px;" class="textClass"
/>

标签: javascript

解决方案


您可以按如下方式更新您的状况。

var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value.trim() == "") {
    alert("empty");
}

如果您想在OccLocation不为空的情况下获得警报,则:

var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value.trim() != "") {
    alert("not empty");
}

推荐阅读