gpt4 book ai didi

jQuery 检查单选按钮是否被选中

转载 作者:行者123 更新时间:2023-12-01 03:12:56 25 4
gpt4 key购买 nike

我有一个表格,如下所示,并带有单选按钮来选择表格行。

<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');
});

错误:

It highlights the selected row but doesnt remove hightlight from previously selected row.

最佳答案

试试这个:

// Table: Add class row selected
$('.table tbody :radio').change(function() {
$('.table tbody :radio:checked').closest('tr').addClass('selected-row');
$('.table tbody :radio:not(:checked)').closest('tr').removeClass('selected-row');
});

关于jQuery 检查单选按钮是否被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22466680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com