【问题标题】: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 中的第一个元素
相对于其兄弟的对象
元素。
在问题的示例中,除了您的单选按钮之外,没有任何“其他”兄弟姐妹。这样就可以了。
在评论的示例中,<br /> 标签是单选按钮的兄弟,因此位于索引 1。这就是为什么您认为 index() 给出了错误的位置