jQuery按名称和ID选择

Ent*_*ity 6 jquery-selectors

我有一张像这样的桌子:

    <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>
Run Code Online (Sandbox Code Playgroud)

我希望能够通过name和选择一个特定的单选按钮id.EG,选择名称Sample2和ID 的单选按钮Sample1.我试过这样做:

    $('[id="Sample1"][name="Sample2"]').checked = true;
Run Code Online (Sandbox Code Playgroud)

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

mcg*_*ilm 8

 $('#Sample1[name="Sample2"]').attr('checked','checked');
Run Code Online (Sandbox Code Playgroud)

但是元素只能有一个id,所以也许你想要class而不是id

  $('.Sample1[name="Sample2"]').attr('checked','checked');
Run Code Online (Sandbox Code Playgroud)

那你的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>
Run Code Online (Sandbox Code Playgroud)

编辑

在这里做了一些改变是一个工作演示