javascript - 禁用无法正常工作 chrome 浏览器

标签 javascript

此代码在 chrome 中运行时无法禁用列表框...但它在 firefox 中工作...是否有任何通用脚本可在所有浏览器中工作

<script>
    function fun()
    {
      //alert("welcome==>"+document.getElementById("geography").value);
         if(document.getElementById("geography").value!=0)
             {

             document.getElementById("country").disabled=true;
             document.getElementById("state").disabled=true;
             }
         else
             {
             document.getElementById("country").disabled=false;
             document.getElementById("state").disabled=false;
            }
             }
             }
    </script>
    </head>
    <body>
     <form name="example" action ="" method="get">
    <table>
    <tr><td>Geography</td> <td><select name="geography"  id="geography" 
    onchange="fun()">
    <option value="0">select</option>
    <option value="1">zone</option>
    <option value="2">state</option>
    <option value="3">city</option>
     </select></td></tr>
    </table>
    <table>
    <tr><td>country</td> <td><select name="country" id="country">
    <option value="0">select</option>
    <option value="1">india</option>
    <option value="2">china</option>
    <option value="3">pak</option>
    </select></td></tr>
    </table>
    <table>
    <tr><td>state</td> <td><select name="state" id="state">
    <option value="0">select</option>
    <option value="1">tamil</option>
    <option value="2">kerala</option>
    <option value="3">andra</option>
      </select></td></tr>
      </body>

请提供解决此问题的任何解决方案...

最佳答案

使用setAttributeremoveAttribute相反:

document.getElementById("country").setAttribute("disabled", "disabled");
document.getElementById("country").removeAttribute("disabled");

此外,辅助函数可能会稍微清理一下代码:

function setDisabledValue(id, value) {
  var elem = document.getElementById(id);
  if(value) elem.setAttribute("disabled", "disabled");
  else elem.removeAttribute("disabled");
}
setDisabledValue("country", true);
...

关于javascript - 禁用无法正常工作 chrome 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18785881/

相关文章:

php - 使用 Jquery 将数据从 javascript 传递到 php

javascript - 如何将变量传递给函数

javascript - 我可以让 onclick 在 mousedown 之后、 mouseup 事件之前触发吗?

java - 使用 .isVisible 方法时无法找到符号错误

javascript - .getDay() 接受什么格式?

Javascript:如何将附加变量传递给 Promise .catch 函数

javascript - ReactJS,如何从 CHILD 更新 PARENT 的状态

javascript - 该脚本必须运行 1 次。如何?

javascript - jQuery刷新列表效果-FadeOut FadeIn问题

javascript - 如何将数组添加到字符串而不在 JS 中展平它们?