简体   繁体   English

JavaScript代码不起作用

[英]JavaScript Code Not Working

My java script code is not working, Currently when you select a value in the first drop down menu the second drop down menu becomes editable which is fine. 我的Java脚本代码不起作用,当前,当您在第一个下拉菜单中选择一个值时,第二个下拉菜单变为可编辑状态,这很好。 But I want to make it as if when you select the option 'default' in the first drop down menu the second drop down menu reverts to 'default' and becomes disabled and read only, but if I select any other option other then 'default' the second menu should be editable. 但是我想让它看起来像是当您在第一个下拉菜单中选择选项“默认”时,第二个下拉菜单恢复为“默认”并变为禁用状态和只读状态,但是如果我选择除“默认”之外的其他任何选项'第二个菜单应该是可编辑的。

    <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>

You should write 你应该写

if(selectedValue == "OptionZero") {

It is the value attribute which set the value on an element. 值属性是在元素上设置值的属性。

Try as 尝试为

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;
   }

}

OR CAN WE TRY AS 或者我们可以尝试为

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 DEMO2

Demo 演示

UPDATE DEMO 更新演示

You need to use value OptionZero and not the Label Default , and set the value back in "mySelection" 您需要使用值OptionZero而不是Label Default ,然后将值重新设置为“ mySelection”

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

You have mistake with the value, this is the answer: 您输入的值有误,这是答案:

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

Change your Script like this 像这样更改脚本

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

Here is Fiddle 这是小提琴

Please go through the following code. 请检查以下代码。

   <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>

Please change selectedValue == "Default" to selectedValue == "OptionZero" and add the following line in if condition document.getElementById("mySelection").value="OptionZero"; 如果条件为document.getElementById(“ mySelection”)。value =“ OptionZero” ;,请将selectedValue ==“ Default”更改为selectedValue ==“ OptionZero”,并在以下行中添加以下行:

Hope it will help you.Thank you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM