如何使用jQuery通过id选择多个元素

How to select multiple elements by id using jQuery

本文关键字:元素 选择 id 何使用 jQuery 通过      更新时间:2023-09-26

我有两个单选按钮:

<p>
    <label for="input_1">Path to Excellent </label> 
    <input id="input_1" name="radio" type="radio" value="1" />
</p>
<p>
    <label for="input_2">Award of Excellence</label> 
    <input id="input_2" name="radio" type="radio" value="2" />
</p>

我想得到val()从其中一个使用他们的id: "input_1"或"input_2" jQuery,像这样的东西:

$('input[#input_1,#input_2]:checked').val();

我的目标是通过id从其中一个被检查的对象中获取val()。我该怎么做呢?

您可以在本例中检查id的开始:

$("input[id^='input_']:checked").val();