javascript - 为什么禁用 select html 后无法启用它?

标签 javascript

这段代码有什么问题?当我将第一个选择更改为国际时,它会禁用第二个选择。但是当我选择“本地”时,它不会启用第二个选择

<script type="text/javascript">
function changetextbox()
{
    if (document.getElementById("LocalOrInternations").value == "International") {
        document.getElementById("NormalOrPaper").disabled='true';
    } 
  else
     document.getElementById("NormalOrPaper").disabled='false';
}
</script>

<span class="input-group-addon" id="basic-addon1">Local Or International</span>
<select name="LocalOrInternations" id="LocalOrInternations" onChange="changetextbox();" class="form-control">
    <option  value="Local">Local</option>
    <option value="International">International</option>
</select>

<span class="input-group-addon" id="basic-addon1">Normal Or Paper Seminar></span>
<select name="NormalOrPaper" id="NormalOrPaper" class="form-control">
    <option  value="Normal">Normal</option>
    <option value="Seminar">Seminar</option>
</select>

Code test

最佳答案

'false''true'字符串。当强制转换为 bool 值时,它们都是 true (因为任何非空白的字符串都是“truthy”。)您需要实际的 bool 值 falsetrue (无引号)。

if (document.getElementById("LocalOrInternations").value == "International") {
    document.getElementById("NormalOrPaper").disabled = true;
    // -------------------------------------------------^^^^
} 
else {
    document.getElementById("NormalOrPaper").disabled = false;
    // -------------------------------------------------^^^^^
}
<小时/>

旁注:我建议与您的 {} 保持一致。要么将它们包含在单个语句中(就像您对 if 所做的那样),要么不包含它们(就像您对 else 所做的那样),但有时不要这样做 将它们包含在单个语句周围,而不是其他时间。

关于javascript - 为什么禁用 select html 后无法启用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43581844/

相关文章:

从文件调用 Javascript 函数

javascript - 使用箭头函数 react this.setState 导致控制台出错

javascript - 如何使用 JavaScript 访问 SharePoint 外部的 SharePoint 数据?

javascript - 一个关于时间复杂度计算和实时消耗的谜题

javascript - 如何验证多个 ng-repeated ng-form

javascript - 如何使用其样式在javascript中复制行

javascript - 在 Handsontable 中访问单元格对齐

javascript - 在 ipython 笔记本中默认隐藏输入单元

javascript - 类型错误 : 'undefined' is not an object (evaluating '$.fn' ) - Wordpress & shuffle. js

javascript - 使用 Ember 存储和模型作为 JqGrid 的源数据