[英]Enable and Disable input field using 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>
您的按鈕正在提交表單,要不提交表單,您必須使用type="button"
按鈕,而且您拼寫錯誤getElementByID
它的getElementById
<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;
}
function toggleEnable(id) { var textbox = document.getElementById(id); if (textbox.disabled) { // If disabled, do this document.getElementById(id).disabled = false; } else { // Enter code here document.getElementById(id).disabled = true; } }
<form> <table> <tr> <td><input id="input1" class="myText" type="text" placeholder="Row 1" /></td> <td><button type="button" onclick="toggleEnable('input1')"> Enable/Disable </button></td> </tr> <tr> <td><input id="input2" class="myText" type="text" placeholder="Row 2" /></td> <td><button type="button" onclick="toggleEnable('input2')"> Enable/Disable </button></td> </tr> </table> </form>
function toggleEnable(el1, el2) { var textbox = document.getElementById(el1, el2); if (textbox.disabled) { // If disabled, do this document.getElementById(el1).disabled = false; document.getElementById(el2).disabled = false; } else { // Enter code here document.getElementById(el1).disabled = true; document.getElementById(el2).disabled = true; } }
<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 2" /></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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.