2014-12-28 59 views
-1

我已經編寫了一些簡單的代碼,可以使按鈕單擊時啓用和禁用輸入字段,但它沒有響應。 請幫我... !!!使用Javascript啓用和禁用輸入字段

<html> 
<head> 
    <script type="text/javascript"> 
     function toggleEnable(el1, el2) { 
      document.getElementByID(el1).disabled = true; 
      document.getElementByID(el2).disabled = true; 
     } 
    </script> 
</head> 
<body> 
    <form> 
     <table> 
      <tr> 
       <td><input id="input1" class="myText" type="text" placeholder="Row 1" /></td> 
       <td><input id="input2" class="myText" type="text" placeholder="Row 1" /></td> 
       <td><button onclick="toggleEnable('input1','input2')"> Enable/Disable </button></td> 
      </tr> 
      <tr> 
       <td><input id="input3" class="myText" type="text" placeholder="Row 3" /></td> 
       <td><input id="input4" class="myText" type="text" placeholder="Row 4" /></td> 
       <td><button onclick="toggleEnable('input3','input4')"> Enable/Disable </button></td> 
      </tr> 
     </table> 
    </form> 
</body> 
</html> 
+1

可能重複http://stackoverflow.com/questions/2874688/how-to-disable-an-inpu噸型文本 – FrEaKmAn

回答

2

你的按鈕提交表單的是,不提交表單,你將不得不使用一個type="button"按鈕,還你拼寫錯誤getElementByIDgetElementById

<form> 
    <table> 
     <tr> 
      <td><input id="input1" class="myText" type="text" placeholder="Row 1" /></td> 
      <td><input id="input2" class="myText" type="text" placeholder="Row 1" /></td> 
      <td><button type="button" onclick="toggleEnable('input1','input2')"> Enable/Disable </button></td> 
     </tr> 
     <tr> 
      <td><input id="input3" class="myText" type="text" placeholder="Row 3" /></td> 
      <td><input id="input4" class="myText" type="text" placeholder="Row 4" /></td> 
      <td><button type="button" onclick="toggleEnable('input3','input4')"> Enable/Disable </button></td> 
     </tr> 
    </table> 
</form> 
function toggleEnable(el1, el2) { 
     document.getElementById(el1).disabled = true; 
     document.getElementById(el2).disabled = true; 
    } 

http://jsfiddle.net/mowglisanu/aam7jg9t/