2013-10-25 45 views
0

當第一個選擇框更改但我不能更改第二個選擇框的禁用樣式時。請幫幫我。如何使用javascript更改選擇框的禁用樣式

<html> 
    <body> 
     <select onchange="a()" id="1"> 
      <option>Choose</option> 
      <option>1</option> 
      <option>2</option> 
     </select> 
     <select id="2" disabled="true"> 
      <option>one</option> 
      <option>two</option> 
     </select> 
     <script> 
      function a(){ 
       if(document.getElementById('1').value!="Choose"){ 
        document.getElementById('2').style.background="yellow"; 
        document.getElementById('2').style.disabled="false"; 
       } 
      } 
     </script> 
    </body> 
</html> 
+0

@ Donte'Trumble你不應該需要這麼簡單的東西小提琴;) –

+0

是啊,這就是爲什麼我刪除了它。 ;) – dowomenfart

回答

0

「disabled」部分是select元素的屬性,而不是CSS屬性。

試試這個:

document.getElementById('2').disabled=false; 
3

disabled是元素的屬性,而不是它的風格集合。

document.getElementById('2').disabled = false; 

同樣重要的是要注意,12不是HTML有效身份證比HTML5,這意味着舊的瀏覽器可能與它嚴重問題(如沒有認識到它作爲一個ID,沒有發現有更早與getElementById,不造型等)我建議給予有意義的ID,即使它只是select1select2,這有助於減少意外複製ID的機會。

0

disabled是屬性,而不是樣式屬性。這應該這樣做:

function a(){ 
    if(document.getElementById('1').value!="Choose"){ 
     document.getElementById('2').style.background = "yellow"; 
     document.getElementById('2').disabled = false; 
    } 
} 
+0

謝謝!我現在沒事。 – user2729868