【发布时间】:2017-02-03 04:52:17
【问题描述】:
我有一个表单,向用户显示单选按钮列表。还有一个选项可以选择某一列中的所有按钮。选择第一次工作正常,但是当我想在两者之间来回切换时,它会中断。
如何一键选中单列中的所有单选按钮?
function checkAll(e) {
if (e.hasClass('allFirst')) {
$('.first').attr('checked', 'checked');
} else {
$('.second').attr('checked', 'checked');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input type="radio" name="A" class="first" />1</td>
<td>
<input type="radio" name="A" class="second" />2</td>
</tr>
<tr>
<td>
<input type="radio" name="B" class="first" />1</td>
<td>
<input type="radio" name="B" class="second" />2</td>
</tr>
<tr>
<td>
<input type="radio" name="C" class="first" />1</td>
<td>
<input type="radio" name="C" class="second" />2</td>
</tr>
</table>
<input type="radio" name="all" class="allFirst" onclick="checkAll($(this));" />Select All #1
<input type="radio" name="all" class="allSecond" onclick="checkAll($(this));" />Select All #2
如果您“全选 #1”,然后“全选 #2”,然后再次“全选 #1”,第三次尝试将不会检查单选按钮。
【问题讨论】:
标签: javascript jquery html