gpt4 book ai didi

jquery-selectors - jQuery 按名称和 ID 选择

转载 作者:行者123 更新时间:2023-12-04 08:35:46 25 4
gpt4 key购买 nike

我有一张像这样的 table :

    <table>
<tr>
<td>
<input type="radio" name="Sample1" id="Sample1" value="1" />
<input type="radio" name="Sample1" id="Sample2" value="1" />
</td>
<td>
<input type="radio" name="Sample2" id="Sample1" value="1" />
<input type="radio" name="Sample2" id="Sample2" value="1" />
</td>
etc....
</tr>
</table>

我希望能够通过 name 选择特定的单选按钮和 id .例如,选择名称为 Sample2 的单选按钮和 ID Sample1 .我尝试这样做:
    $('[id="Sample1"][name="Sample2"]').checked = true;

但没有运气......我应该怎么做?

最佳答案

 $('#Sample1[name="Sample2"]').attr('checked','checked');

但是元素只能有一个 id 所以也许你想要 class 而不是 id
  $('.Sample1[name="Sample2"]').attr('checked','checked');

然后你的html
<table>
<tr>
<td>
<input type="radio" name="Sample1" class="Sample" value="1" />
<input type="radio" name="Sample1" class="Sample" value="1" />
</td>
<td>
<input type="radio" name="Sample2" class="Sample1" value="1" />
<input type="radio" name="Sample2" class="Sample2" value="1" />
</td>
</tr>
</table>

编辑

在这里做了一些更改 working demo

关于jquery-selectors - jQuery 按名称和 ID 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5636513/

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