2014-10-10 159 views
0

我的java脚本代码不工作,当前当您在第一个下拉菜单中选择一个值时,第二个下拉菜单变为可编辑,这很好。但是我想让它看起来好像在第一个下拉菜单中选择“默认”选项时,第二个下拉菜单将恢复为“默认”并且变为禁用且只读,但如果我选择其他选项,则选择“default '第二个菜单应该是可编辑的。JavaScript代码不能正常工作

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

该值不是“默认”,它是“OptionZero” – galchen 2014-10-10 12:09:39

回答

1

你应该写

if(selectedValue == "OptionZero") { 

它是设置元素的值属性的值。

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

+0

如何使第二个下拉菜单在禁用时回到默认值?所以,如果我启用第二个下拉列表并选择optionOne = A,但然后在第一个下拉列表中选择默认值,我如何使第二个下拉列表回到默认值并使其成为只读 – 2014-10-10 12:13:40

+0

@HassanChaudhry检查我的更新答案 – Amit 2014-10-10 12:15:33

0

您需要使用值OptionZero而不是标签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; 
} 
0

你有错与价值,这就是答案:

function mySecondFunction() { 
    var ddl = document.getElementById("1"); 
    var selectedValue = ddl.options[ddl.selectedIndex].value; 
    document.getElementById("mySelection").disabled = selectedValue === "OptionZero"?true:false; 
} 
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

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

希望它能帮助你。谢谢。