我有一个复选框列表,每个复选框都有一个输入字段。如果我选中该复选框,则必须禁用输入字段。 示例:
Checkbox 1 - Input 1
Checkbox 2 - Input 2
Checkbox 3 - Input 3
真正的代码:
<table id="food" width="580px">
<tr>
<th colspan="5">Eten</th>
<tr>
<td><input type="checkbox" name="checkbox_1_1" value="" /></td>
<input type="hidden" name="todo_1_1" value="7" />
<td>Braadworst</td>
<td>7</td>
<td><input type="text" name="item_1_1" size="4" value="" /></td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox_1_2" value="" /></td>
<input type="hidden" name="todo_1_2" value="5" />
<td>Witte worst</td>
<td>5</td>
<td><input type="text" name="item_1_2" size="4" value="" /></td>
<td></td>
</tr>
</tr>
</table>
只能禁用具有相同编号的输入字段...
通过谷歌我发现:http://techchorus.net/disable-and-enable-input-elements-div-block-using-jquery
该示例运行完美,但是有没有一种方法可以在不预先定义名称的情况下执行此操作?在我的例子中,不可能知道名字,所以必须在切换复选框时确定它们,不是吗?
有什么建议吗?
最佳答案
如果您有这种“非常”简单的结构
<input type="checkbox" name="" /><input type="text" name="" />
<input type="checkbox" name="" /><input type="text" name="" />
<input type="checkbox" name="" /><input type="text" name="" />
<input type="checkbox" name="" /><input type="text" name="" />
<input type="checkbox" name="" /><input type="text" name="" />
可以
$(':checkbox').change(function(){
$(this).next().attr('disabled',!this.checked);
});
但是我知道你没有那种“非常”简单的结构,所以,阅读下面的内容并从上面得到想法......
- > traversing
- > selectors
如果你能提供你的 html 的结构,那就更好了......
关于javascript - 复选框切换时禁用输入字段(JQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3109584/