javascript - 查找在jquery中选中的单选按钮的值

标签 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');
        }
      }
);

最佳答案

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

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/

相关文章:

javascript - Ui路由器刷新问题

javascript - React-router hashHistory 推送仅更改 URL

jquery dataTables - 如何提取行数据 onclick 事件

javascript - jQuery Booklet 插件翻转一次后停止自动播放

javascript - jquery 选项卡中的 morris.js 图表

javascript - jQuery:当鼠标静止时不重复动画/不安全动画队列/停止动画

javascript - 将 slider 输入值固定在气泡中

javascript - 标签计数器

javascript - process.stdout 在 childProcess 中没有函数 "clearLine"

Javascript方法不会被执行