【问题标题】:jQuery RadioButton indexjQuery 单选按钮索引
【发布时间】:2011-01-29 08:29:44
【问题描述】:

如何使用 jQuery 获取选中的 RadioButton 索引?我需要阅读它并保存到 cookie 以便稍后使用代码加载状态

$('input[name="compression"]')[{Reading from cookies code}].checked = true;



1<input name="compression" type="radio" value="1" checked="checked" />
2<input name="compression" type="radio" value="2" />
3<input name="compression" type="radio" value="3" />

问候 托马斯

【问题讨论】:

    标签: jquery indexing radio-button


    【解决方案1】:

    这将返回所选单选按钮的从零开始的索引

    $('input[name=compression]:checked').index()
    

    对于注释中描述的情况使用(通常是正确的语法)

    $('input[name=porient]:checked').index('input[name=porient]')
    

    如果你想知道为什么第一个给出错误的结果,你应该阅读.index() documentation

    如果没有参数传递给 .index() 方法,返回值为 表示位置的整数 jQuery 中的第一个元素 相对于其兄弟的对象 元素。

    在问题的示例中,除了您的单选按钮之外,没有任何“其他”兄弟姐妹。这样就可以了。

    在评论的示例中,&lt;br /&gt; 标签是单选按钮的兄弟,因此位于索引 1。这就是为什么您认为 index() 给出了错误的位置

    【讨论】:

    • 奇怪的事情,它在上面列出的代码的“压缩”单选按钮上工作正常,但在下面的“porient”单选按钮代码上它返回错误的索引。如果第一个单选按钮被选中,它返回 0(好的)它第二个选择它返回 2(!!!!)不明白发生了什么。有任何想法吗?

      人像
      风景

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多