我写了一些简单的代码,可以在单击按钮时启用和禁用输入字段,但它没有响应。 请帮助我...!!!
<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;
}
关于javascript - 使用 Javascript 启用和禁用输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677575/