javascript - 为什么我不能在 javascript 中使用 && 来表示非空白和非空格

标签 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"
/>

最佳答案

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

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

如果您想在 OccLocation 不为空时收到警报:

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

关于javascript - 为什么我不能在 javascript 中使用 && 来表示非空白和非空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53207061/

相关文章:

javascript - 使用 javascript 和 jquery 从表单中的文件类型输入上传文件数据

javascript - 正则表达式匹配同一单词的不同版本

javascript - jQuery 事件处理逻辑

javascript - 如何隐藏 HTML 元素集合,直到页面加载完成?

javascript - 如何解析位置:fixed for a bottom toolbar on iOS (iPhone/iPad)

javascript - 如何将 d3.svg.axis 限制为整数标签?

javascript - 错误类型错误 : Cannot read property 'invalid' of undefined while trying to list FormArray of FormGroups in Angular

Javascript 弹出窗口作为单例

javascript - 使用 chrome.storage.local 存储数组

javascript - javascript中的同步调用,但未处理同步