2014-10-10 209 views
0

如何修改代碼,以便當我從第一個下拉菜單中選擇一個選項時,第二個下拉菜單變爲可編輯,否則第二個下拉菜單應該是隻讀的,如果選項在第一個下拉菜單中是「默認」,那麼第二個下拉菜單應該是隻讀的。Javascript代碼不起作用

<!DOCTYPE html> 
<html> 

<head> 
<script type="text/javascript"> 
function mySecondFunction() { 
    document.getElementById("mySelection").disabled = false; 
} 

</script> 

</head> 
<body> 

<form action=""> 
<select name="cars" onload="mySecondFunction()"> 
<option value="default">default</option> 
<option value="saab">Saab</option> 
<option value="fiat">Fiat</option> 
<option value="audi">Audi</option> 
</select> 
<br> 
<select disabled name="cars" id="mySelection"> 
<option value="Action1">Volvo</option> 
<option value="Action2">Saab</option> 
<option value="Action3">Fiat</option> 
<option value="Action4">Audi</option> 
</select> 
</form> 

</body> 
</html> 

回答

1

看看首都:

document.getElementById("MySelection").disabled = false; 
<select disabled name="cars" id="mySelection"> 

首先是與 「M」,並與

+0

仍然沒有工作 – user3916285 2014-10-10 10:10:41

+0

http://jsfiddle.net/se22kfsr/ – 2014-10-10 10:12:02

+0

看不工作 - 第二個下拉菜單應成爲可編輯的您已選擇在首次下降的一個選項後,下拉菜單 – user3916285 2014-10-10 10:13:37

1

您有mySelection

拼錯MySelection在JS 「m」 的seccond你有document.getElementById("MySelection") // notice the uppercase M

並在HTML你hav e id="mySelection"

另外當窗口加載時將第二個選擇設置爲false,並將第一個onChange事件添加到第一個選擇,將第二個事件設置爲啓用/ true ...這是你想要的嗎?

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 

window.onload = function(){ 
    document.getElementById("mySelection").disabled = true; 
}; 

function mySecondFunction() { 
    document.getElementById("mySelection").disabled = false; 
} 

</script> 

</head> 
<body> 

<form action=""> 
<select name="cars" onchange="mySecondFunction()"> 
<option value="default">default</option> 
<option value="saab">Saab</option> 
<option value="fiat">Fiat</option> 
<option value="audi">Audi</option> 
</select> 
<br> 
<select name="cars" id="mySelection"> 
<option value="Action1">Volvo</option> 
<option value="Action2">Saab</option> 
<option value="Action3">Fiat</option> 
<option value="Action4">Audi</option> 
</select> 
</form> 

</body> 
</html> 

您可能需要設置第二選擇回禁用如果用戶選擇「默認」第一菜單變化值

+0

仍然不起作用 – user3916285 2014-10-10 10:10:00

0

JavaScript的選擇後是區分大小寫的。你的錯誤在mySelection id:

document.getElementById("MySelection").disabled = false; 
<select disabled name="cars" id="mySelection"> 
0

你必須在body標籤中使用它。 fiddle

<head> 


</head> 
<body onload="mySecondFunction()"> 

<form action=""> 
<select name="cars" > 
<option value="default">default</option> 
<option value="saab">Saab</option> 
<option value="fiat">Fiat</option> 
<option value="audi">Audi</option> 
</select> 
<br> 
<select disabled name="cars" id="mySelection"> 
<option value="Action1">Volvo</option> 
<option value="Action2">Saab</option> 
<option value="Action3">Fiat</option> 
<option value="Action4">Audi</option> 
</select> 
</form> 

</body> 
<script type="text/javascript"> 

function mySecondFunction() { 
alert("aaa"); 
    document.getElementById("mySelection").disabled = false; 
} 

</script> 
</html> 
0

正如有人指出你拼寫錯誤的第二<select>的ID,但也使用了錯誤的事件,onchange是你想要的,而不是什麼onload

function mySecondFunction() { 
 
    document.getElementById('mySelection').disabled = false;  
 
}
<form action=""> 
 
    <select name="cars" onchange="mySecondFunction();"> 
 
     <option value="default">default</option> 
 
     <option value="saab">Saab</option> 
 
     <option value="fiat">Fiat</option> 
 
     <option value="audi">Audi</option> 
 
    </select> 
 
    
 
    <br /> 
 
     
 
    <select disabled="disabled" name="cars" id="mySelection"> 
 
     <option value="Action1">Volvo</option> 
 
     <option value="Action2">Saab</option> 
 
     <option value="Action3">Fiat</option> 
 
     <option value="Action4">Audi</option> 
 
    </select> 
 
</form>

但考慮使用jQuery和附上事件的JavaScript而不是讓它們存儲在HTML中。

0

試試這個:

更換

document.getElementById("mySelection").disabled = false; 

與此

document.getElementById("mySelection").removeAttribute('disabled'); 

此外,您可能要更改

onload="mySecondFunction()" 

與此:

onchange="mySecondFunction()" 

希望這會有所幫助。

0

,如果你想刪除禁用像(薩博)

See fiddle 注意到一個特定的值:把腳本B4 head標籤

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

<form action=""> 
    <select id="1" name="cars" onchange="mySecondFunction()"> 
     <option value="default">default</option> 
     <option value="saab">Saab</option> 
     <option value="fiat">Fiat</option> 
     <option value="audi">Audi</option> 
    </select> 

    <br /> 

    <select disabled="disabled" name="cars" id="mySelection"> 
     <option value="Action1">Volvo</option> 
     <option value="Action2">Saab</option> 
     <option value="Action3">Fiat</option> 
     <option value="Action4">Audi</option> 
    </select> 
</form> 

UPDATE See fiddle

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

如何做到這一點,如果它的默認值,然後第二個下拉菜單保持禁用,而如果其他任何其他值默認第二個下拉菜單應該是可編輯的 – user3916285 2014-10-10 10:47:46

+0

只需將更改值更改爲默認值並切換爲假和真 函數mySecondFunction() {dd} = document.getElementById(「1」); var selectedValue = ddl.options [ddl.selectedIndex] .value; if(selectedValue ==「default」) { document.getElementById(「mySelection」)。disabled = true;其它{ document.getElementById(「mySelection」)。disabled = false; } }' – Jonathan 2014-10-11 01:27:49

+0

查看上面的更新 – Jonathan 2014-10-11 01:59:14

0

將onload更改爲onchange。

<select name="cars" onchange="mySecondFunction();">