jQuery单选按钮索引

jQuery单选按钮索引,jquery,indexing,radio-button,Jquery,Indexing,Radio Button,如何使用jQuery检查RadioButton索引?我需要读取它并保存到cookies,以便稍后使用代码加载状态 $('input[name="compression"]')[{Reading from cookies code}].checked = true; 1<input name="compression" type="radio" value="1" checked="checked" /> 2<input name="compression" type="r

如何使用jQuery检查RadioButton索引?我需要读取它并保存到cookies,以便稍后使用代码加载状态

$('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" />
$('input[name=“compression”])[{从cookies代码读取].checked=true;
1.
2.
3.
问候
Tomas

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

$('input[name=compression]:checked').index()
对于注释用法中描述的情况(通常使用正确的语法)

如果你想知道为什么第一个给出了错误的结果,你应该阅读

如果没有参数传递给 .index()方法,返回值为 一个整数,指示 jQuery中的第一个元素 对象相对于其同级对象 元素

在问题的样本中,除了你的单选按钮外,没有任何“其他”兄弟姐妹。因此它是有效的


在注释的示例中,

标记是单选按钮的同级,因此位于索引1中。这就是为什么您认为
index()
给出了错误的位置

奇怪的是,它在上面列出的代码的“压缩”单选按钮上运行良好,但在下面的代码中,对于“客户端”单选按钮,它返回了错误的索引。如果选择第一个单选按钮,则返回0(确定)。如果选择第二个单选按钮,则返回2(!!!!)无法理解发生了什么。有什么想法吗 纵向
横向

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