【发布时间】:2014-04-23 09:14:36
【问题描述】:
我有一个如下表格,并带有单选按钮来选择表格行。
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td>
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
</label>
</td>
<td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/02.jpg"></td>
</tr>
<tr class="selected-row">
<td>
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio" checked>
</label>
</td>
<td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/01.jpg"></td>
</tr>
<tr>
<td>
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
</label>
</td>
<td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/03.jpg"></td>
</tr>
<tr>
<td>
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
</label>
</td>
<td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/04.jpg"></td>
</tr>
<tr>
<td>
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
</label>
</td>
<td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/05.jpg"></td>
</tr>
</tbody>
</table>
</div>
现在,当我选择一个单选按钮时,我想突出显示该行并从其他行中删除突出显示。
// Table: Add class row selected
$('.table tbody :radio').on('check uncheck toggle', function (e) {
var $this = $(this),
check = $this.prop('checked'),
toggle = e.type == 'toggle',
checkboxes = $('.table tbody :radio'),
checkAll = checkboxes.length == checkboxes.filter(':checked').length
$this.closest('tr')[check ? 'addClass' : 'removeClass']('selected-row');
if (toggle) $this.closest('.table').find('.toggle-all :radio').checkbox(checkAll ? 'check' : 'uncheck');
});
错误:
它突出显示选定的行,但不会从中删除突出显示 之前选择的行。
【问题讨论】:
标签: jquery html radio-button checked