2014-10-10 137 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」。

希望它能幫助你。謝謝。