【发布时间】:2018-03-06 11:17:31
【问题描述】:
我想制定一个逻辑,如果选中子复选框,它也会检查父复选框。输入可以是父级、子级和两者:
<input type="checkbox" name="checkbox[$counter]" class="parent" />
<input type="checkbox" name="checkbox[$counter]" class="child" />
<input type="checkbox" name="checkbox[$counter]" class="parent child" />
这个结构:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 5
会是这样的:
<table class="table">
<tr>
<td>
<input type="checkbox" name="checkbox[1]" class="parent" />
</td>
<td>
.
.
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox[2]" class="child" />
</td>
<td>
.
.
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox[3]" class="child" />
</td>
<td>
.
.
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox[4]" class="parent" />
</td>
<td>
.
.
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox[5]" class="child parent" />
</td>
<td>
.
.
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox[6]" class="child" />
</td>
<td>
.
.
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox[7]" class="child" />
</td>
<td>
.
.
</tr>
</table>
如果我检查 3,它会检查 1,如果我检查 6,它也会检查 5 和 4。
复选框也位于单独的表格行中,因为我想在复选框旁边的表格中显示一些结构化的信息。
你能告诉我一个脚本是做什么的吗?我试过了,但它不起作用:
$(document).ready(function() {
$('input.child').change(function() {
if ($(this).is(':checked')) {
$(this).closest('input.parent:checkbox').attr('checked', true);
}
});
});
【问题讨论】:
-
是li ui结构吗?
-
checkbox不是chechkbox -
@programtreasures table tr td 结构
-
@Pedram 对不起,错字
-
您的
htmlstructure很重要,因为这些答案在这里没有任何表格,您需要使用表格结构更新您的问题。
标签: javascript jquery checkbox input