如果未选中复选框,则禁用html中的文本输入,反之亦然
disabling a text input in html if a checkbox is not checked and vice versa using JQUERY
我有一个html表格有不同的行和列,文本输入有一个类类似于复选框的值
如果复选框未被选中,我想禁用与复选框同一行的文本输入
这是html
<table>
<tr>
<td><input type="checkbox" name="new" value="37"></td>
<td><input type="text" name="new" id="quantity" class="37"></td>
</tr>
<tr>
<td><input type="checkbox" name="new" value="38"></td>
<td><input type="text" name="new" id="quantity" class="38"></td>
</tr>
.....#this continue
</table>
我想检查复选框是否被选中,因此禁用输入
如何继续使用jquery
使用以下代码:
$(":checkbox[class=ch]").on("change",function(){
$("input[class = " + $(this).attr("value") + "]").prop("disabled",!$(this).prop("checked"));
})
最终代码:
<html>
<title>This is test</title>
<head>
<style>
</style>
</head>
<body>
<table border="1">
<tr>
<td><input class="ch" type="checkbox" name="new" value="37"></td>
<td><input type="text" name="new" id="quantity" class="37" disabled></td>
</tr>
<tr>
<td><input class="ch" type="checkbox" name="new" value="38"></td>
<td><input type="text" name="new" id="quantity" class="38" disabled></td>
</tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(":checkbox[class=ch]").on("change",function(){
$("input[class = " + $(this).attr("value") + "]").prop("disabled",!$(this).prop("checked"));
})
})
</script>
</body>
</html>
尝试:
$('input[type=checkbox]').each(function(){
if($(this)){
$(this).siblings("input[type=text]").prop('disabled', false);
}
else{
$(this).siblings("input[type=text]").prop('disabled', true);
}
});
$('input[type=checkbox]').click(function(){
if($(this)){
$(this).siblings("input[type=text]").prop('disabled', false);
}
else{
$(this).siblings("input[type=text]").prop('disabled', true);
}
});
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何设置html元素填充的动画
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何编写HTML输入的JS内联
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量
- 如何使Javascript动态html表及其上的事件
- 在chrome.tabs.onCreated之后加载HTML页面
- 如何在vs2002中调试html页面
- 如何使用javascript或html下载PDF格式的填写表单
- 视频HTML没有'无法在Internet Explorer 11上工作
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- Jquery/Javascript 解决方案,用于将 wiki 文本转换为 HTML,反之亦然
- 将 CSS 样式规则转换为 HTML 元素(反之亦然)
- 如何将数字转换为货币,反之亦然?(html输入/输出)
- 在HTML画布/Javascript上捕获用户输入,反之亦然
- 如果未选中复选框,则禁用html中的文本输入,反之亦然