JavaScript代码不起作用

时间:2014-10-10 12:06:19

标签: javascript html

我的java脚本代码无效。目前,当您在第一个下拉菜单中选择一个值时,第二个下拉菜单变得可编辑,这很好。但我想让它好像当你在第一个下拉菜单中选择'default'选项时,第二个下拉菜单恢复为'default'并被禁用并且只读,但是如果我选择其他任何选项,那么'default '第二个菜单应该是可编辑的。

    <html>
<head>
<script type="text/javascript">
function mySecondFunction() {
 var ddl = document.getElementById("1");
 var selectedValue = ddl.options[ddl.selectedIndex].value;
    if (selectedValue == "Default")
   {
    document.getElementById("mySelection").disabled = true;
   }else {
    document.getElementById("mySelection").disabled = false;
   }

}
</script>

</head>
<body>
<select  id="1" onchange="mySecondFunction()">
                <option value="OptionZero">Default</option>
                <option value="OptionOne">Car</option>
                <option value="OptionTwo">Car2</option>
                <option value="OptionThree">Car23</option>
</select>
<br>
<select disabled id="mySelection">
                <option value="OptionZero">Default</option>
                <option value="OptionOne">A</option>
                <option value="OptionTwo">B</option>
                <option value="OptionThree">C</option>
        </select>


</body>
</html>

6 个答案:

答案 0 :(得分:1)

你应该写

if(selectedValue == "OptionZero") {

这是value属性,用于设置元素的值。

答案 1 :(得分:0)

尝试

function mySecondFunction() {
  var ddl = document.getElementById("1");
  var selectedValue = ddl.options[ddl.selectedIndex].value;
  if (selectedValue == "OptionZero")
    {
     document.getElementById('mySelection').selectedIndex = 0;
      document.getElementById("mySelection").disabled = true;
   }else {
    document.getElementById("mySelection").disabled = false;
   }

}

或者我们可以尝试

function mySecondFunction() {
 var ddl = document.getElementById("1");
 var selectedValue = ddl.options[ddl.selectedIndex].text;
    if (selectedValue == "Default")
   {
    document.getElementById('mySelection').selectedIndex = 0;
    document.getElementById("mySelection").disabled = true;
   }else {
    document.getElementById("mySelection").disabled = false;
   }

}

DEMO2

Demo

UPDATE DEMO

答案 2 :(得分:0)

您需要使用值OptionZero而不是标签Default,并将值设置回&#34; mySelection&#34;

if (selectedValue == "OptionZero")
{
    document.getElementById("mySelection").disabled = true;
    // set the value to default
    document.getElementById("mySelection").value = "OptionZero"
} else {
    document.getElementById("mySelection").disabled = false;
}

答案 3 :(得分:0)

你错了价值,这就是答案:

function mySecondFunction() {
  var ddl = document.getElementById("1");
  var selectedValue = ddl.options[ddl.selectedIndex].value;
  document.getElementById("mySelection").disabled = selectedValue === "OptionZero"?true:false;
}

答案 4 :(得分:0)

像这样更改你的剧本

function mySecondFunction() {
    if (document.getElementById("1").value == "OptionZero")
   {
    document.getElementById("mySelection").value="OptionZero";
    document.getElementById("mySelection").disabled = true;
   }else {       
    document.getElementById("mySelection").disabled = false;
   }
}

这是Fiddle

答案 5 :(得分:0)

请仔细阅读以下代码。

   <html>
  <head>
  <script type="text/javascript">
  function mySecondFunction() {
  var ddl = document.getElementById("1");
  var selectedValue = ddl.options[ddl.selectedIndex].value;
 if (selectedValue == "OptionZero")
  {
  document.getElementById("mySelection").value="OptionZero";
  document.getElementById("mySelection").disabled = true;
  }else {
  document.getElementById("mySelection").disabled = false;
  }

 }
</script>
</head>
<body>
<select  id="1" onchange="mySecondFunction()">
            <option value="OptionZero">Default</option>
            <option value="OptionOne">Car</option>
            <option value="OptionTwo">Car2</option>
            <option value="OptionThree">Car23</option>
</select><br>
 <select disabled id="mySelection">
            <option value="OptionZero">Default</option>
            <option value="OptionOne">A</option>
            <option value="OptionTwo">B</option>
            <option value="OptionThree">C</option>
    </select>
</body>
</html>

请改变         selectedValue ==“默认” 至         selectedValue ==“OptionZero” 并在if条件中添加以下行     。的document.getElementById( “mySelection”)值= “OptionZero”;

希望它会对你有所帮助。谢谢。