使用 Javascript 启用和禁用输入字段
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;
}
http://jsfiddle.net/mowglisanu/aam7jg9t/
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>
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在HTML输入字段中添加不可删除的后缀
- 互斥单选按钮和相应的输入字段
- 在IE9中的输入字段中输入焦点最近按钮
- 选中单选框时将属性添加到输入字段
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 输入字段将't获取更新的值
- 如何选择多个输入字段并删除所需的属性
- 输入字段,只接受0到12之间的数字
- 单击鼠标,用MySQL数据填充html表单输入字段
- 使用jquery将输入字段转换为文本
- Model中的Typeahead返回空值以形成输入字段
- 如何从查询字符串中的输入字段发回文本
- 如何验证日期、月份和日期的3个独立输入字段;年使用jquery或javascript
- 将值传递给jquery创建的输入字段