jQuery:如何获取选中单选按钮的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5395537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: how to get the index of a checked radio button
提问by Mass Dot Net
I recently came across a StackOverflow answer that gave excellent instructions on how to get the value of a checked radio button using jQuery:
我最近遇到了一个 StackOverflow 答案,它提供了关于如何使用 jQuery 获取选中单选按钮的值的出色说明:
var radioVal = $("#myFormID input:radio[name='radioFieldName']:checked").val();
alert('Selected radio button value = ' + radioVal);
Now I'm trying to find the zero-based index of the checked radio button. I thought it would be relatively simple:
现在我试图找到选中单选按钮的从零开始的索引。我认为这会相对简单:
var radioIdx = $("#myFormID input:radio[name='radioFieldName']:checked").index();
However, radioIdx
is always returning a value of -1
. Any ideas on what I might be doing wrong?
但是,radioIdx
始终返回 的值-1
。关于我可能做错了什么的任何想法?
回答by Kelsey
This should work. You could do it all in one line but I broke it up to make it easier to read:
这应该有效。您可以在一行中完成所有操作,但我将其拆分以使其更易于阅读:
var radioButtons = $("#myFormID input:radio[name='radioFieldName']");
var selectedIndex = radioButtons.index(radioButtons.find(':checked'));
EDIT:Verify that your selector is correct. Break it down step by step:
编辑:验证您的选择器是否正确。一步一步分解:
var radioButtons = $("#myFormID input:radio[name='radioFieldName']");
// this should contain the count of all your radio buttons
var totalFound = radioButtons.length;
// this should contain the checked one
var checkedRadioButton = radioButtons.find(':checked');
// this should get the index of the found radio button based on the list of all
var selectedIndex = radioButtons.index(checkedRadioButton);
Which step is not producing the expected value in these?
其中哪一步没有产生预期值?
EDIT:To show final solution
编辑:显示最终解决方案
var radioButtons = $("#myFormID input:radio[name='radioFieldName']");
var selectedIndex = radioButtons.index(radioButtons.filter(':checked'));
回答by tvanfosson
Try using the form of indexthat allows you to specify an element in a collection and returns it's relative position in the collection:
尝试使用索引的形式,它允许您在集合中指定一个元素并返回它在集合中的相对位置:
var radioIdx = $(":radio[name='radioFieldName']")
.index($(":radio[name='radioFieldName']:checked"));
or the version that finds the first item matching a selector that is in another collection and reports it's position in the second collection.
或者找到与另一个集合中的选择器匹配的第一个项目并报告它在第二个集合中的位置的版本。
var radioIdx = $(":radio[name='radioFieldName']:checked")
.index(":radio[name='radioFieldName']");
回答by Tejs
There are a couple of options:
有几个选项:
1) Enumerate through the radio button list (aka without the :checked modifier) and test to see if it is checked; if so, you have the id of the element in the group.
1) 枚举单选按钮列表(也就是没有 :checked 修饰符)并测试它是否被选中;如果是这样,则您拥有组中元素的 ID。
2) The better way (imo), is to simply associate some data with each element and pull that data. So if you give the element a 'data-index' attribute with the value, you can then simply call
2)更好的方法(imo)是简单地将一些数据与每个元素相关联并提取该数据。因此,如果您给元素一个带有值的 'data-index' 属性,您就可以简单地调用
$('#myFormID input:radio[name='radioFieldName']:checked').data('index')
And have the value.
并且有价值。
回答by atabak
use this
用这个
alert($("input[name=checkname]:checked").map(function () {return this.value;}).get().join(","));
回答by Obi
Though this is old, but try this (assuming you're within the click function of the radio buttons)
虽然这是旧的,但试试这个(假设你在单选按钮的点击功能内)
$('#myFormID input:radio[name=radioFieldName]').click(function(){
var index = $('#myFormID input:radio[name=radioFieldName]').index(this);
alert(index);
});
回答by Bhimbim
it return -1 because you get the value before any radiobutton was checked. i think you miss placed your listener and index checked. I just got the same error, but finally found the answer, i get the index before anything was checked, please check your code arrangement, the code stated above is right.
它返回 -1,因为您在检查任何单选按钮之前获得了该值。我想你错过了你的听众和索引检查。我刚刚遇到了同样的错误,但终于找到了答案,我在检查任何内容之前得到了索引,请检查您的代码安排,上面的代码是正确的。