我有一张包含 3 个单选按钮的表格,我想知道某个特定的单选按钮在给定时间点是否处于选中状态。
<table align="center" width="100%" border="0">
<tbody>
<tr>
<td>
<input type="radio" name="compareRadio" value="all" checked="checked"/>
<label>View All Records</label>
</td>
<td>
<input type="radio" name="compareRadio" value="diff" />
<label>View Differences</label>
</td>
<td>
<input type="radio" name="compareRadio" id="patch" value="patches" />
<label>Compare Patches</label>
</td>
<td>
<input type="button" class="btn" value="Export Into Excel"/>
</td>
</tr>
</tbody>
</table>
我向服务器发送请求,当结果返回时我想确定是否选择了 patches
单选按钮。
所以我做了类似的事情..但它返回all
单选按钮
$.post("/csm/compare.action",
{
sessiontoken: sessiontoken,
compareCategory: "system",
compareSubCategory:"patch",
xml1:absP[0],
xml2:absP[1]},
function(resdata)
{
comparePatchData=resdata;
comparePatchLoading=false;
if($("input:radio[name=compareRadio]").val()=="patches")
{
//Trigger click on radio button for "same" campare
$('input[name=compareRadio]:eq(2)').click(); //so that it refreshes the content
$("input[name=compareRadio]:eq(2)").attr("checked", true);
$('input[type="radio"]').removeAttr('disabled');
}
}
);
最佳答案
如果你只是想知道它是否被选中,那么你可以这样做:
if($('#patch:checked').length)
// It is checked.
或者:
if($('input[value=patches]:checked').length)
// It is checked.
$()
函数返回匹配元素的数组,因此您可以检查其 length
属性以查看匹配的元素数量(如果有)。
引用资料:
关于javascript - 查找在jquery中选中的单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6703461/