使用JS通过复选框启用文本框
Enabling textbox by means of checkbox with JS
您能解释一下如何根据组合框启用/禁用文本框(也就是说,可以编辑或不能编辑)吗?如果组合框值=1->文本框启用;如果组合框=0->文本框禁用。
var combo = document.getElementById('combo');
combo.onchange = function() {
var value = document.getElementById('combo').value;
var textBox = document.getElementById('textbox');
if (value == '0') {
textBox.setAttribute('readonly', 'readonly');
} else if (value == '1') {
textBox.removeAttribute('readonly');
}
};
这是一个现场演示。
您想要使用select
元素的onchange
事件。这里有一个例子:
<html>
<head>
<script type="text/javascript">
function select1changed()
{
var select1 = document.getElementById('select1');
var txt1 = document.getElementById('txt1');
txt1.disabled = (select1.value == '0');
}
</script>
</head>
<body>
<form>
Combo:
<select id="select1" onchange="select1changed();">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
</select>
Text: <input id="txt1" type="text" value="sometext" disabled="disabled" />
</form>
</body>
注意:我将txt1
标记为禁用,以处理加载页面时的初始状态,并且select1
设置为Option 0
。
检查这个,
<select id='abc' onChange="changeMe()">
<option value="">--Select--</option>
<option value="111">111</option>
<option value="222">222</option>
</select>
<input type="text" name="abcText" />
<script>
function changeMe(){
if(document.getElementById("abc").value == "111"){
document.getElementById("abcText").disabled = true;
}else{
document.getElementById("abcText").disabled = false;
}
}
</script>
是的,你可以很容易地做到。
使用类似的文本框的disabled
属性
function enableField(val)
{
document.form1.address2.disabled = val;
}
注意:address2
是一个文本字段
相关文章:
- 使用javascript Without Ajax/Jquery在特定行中启用文本框
- 单击每个表格行中的复选框时启用文本框
- 禁用基于下拉列表选择 asp.net JavaScript 启用文本框
- 根据下拉列表中的选定值启用文本框
- 重新启用文本选择(Opera、webkit等)
- 根据输入启用文本框
- 通过JS功能在中继器中启用文本框
- 启用文本框选中复选框数组
- 如何在特定行中启用文本框
- 单选按钮未启用文本框
- 启用文本输入
- 与disableSelection功能相反,后者用于为特定HTML元素启用文本选择
- ASP.NET MVC Checboxes禁用/启用文本框
- 无法使用mootools启用文本框
- 当下拉框中的值被选中时,启用文本框
- 选中单选按钮导致在asp.net中启用文本框
- 如何在ChromeOS应用程序窗口中启用文本选择
- 禁用/启用文本框,单击单选按钮
- 使用javascript启用文本框
- 启用文本选择javascript