gpt4 book ai didi

jquery - 单击 td,选择 jQuery 中的单选按钮

转载 作者:行者123 更新时间:2023-12-03 22:35:47 24 4
gpt4 key购买 nike

一些非常基本的 jQuery 遇到了一个小问题,我希望能够单击表格单元格,然后让它自动选择其中的单选按钮。

HTML:

  <table>
<tr>
<td>
1<br>
<input type="radio" name="choice" value="1">
</td>
<td>
2<br>
<input type="radio" name="choice" value="2">
</td>
<td>
3<br>
<input type="radio" name="choice" value="3">
</td>
</tr>
</table>

jQuery:

 $("td").click(function () {

$(this).closest('input:radio').attr('checked',true);

});

如有任何帮助,我们将不胜感激,谢谢!

最佳答案

使用此选择器:

$('input:radio', this).attr('checked', true);

或者使用find方法:

$(this).find('input:radio').attr('checked', true);

您的代码应如下所示:

$("td").click(function () {
$(this).find('input:radio').attr('checked', true);
});

关于jquery - 单击 td,选择 jQuery 中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4625037/

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