找到jquery中选中的单选按钮的值

时间:2011-07-15 06:31:26

标签: javascript jquery

我有这个包含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');
        }
      }
);

2 个答案:

答案 0 :(得分:3)

如果您只是想知道它是否被选中,那么您可以这样做:

if($('#patch:checked').length)
    // It is checked.

或者:

if($('input[value=patches]:checked').length)
    // It is checked.

$()函数返回匹配元素的数组,以便您可以检查其length属性以查看匹配的内容(如果有)。

参考文献:

答案 1 :(得分:1)

要查明是否选中了特定复选框,您可以使用jQuery的is()

if($('input[value=all]').is(':checked'))
{
   //Yep, it's checked
}
相关问题