当我迭代输入集合时,如果输入是一组 radio 的一部分,如何获取当前 radio 索引?
$('input').each(function(){
if($(this).is(':radio')){
// here get the index of the radio in the radio group,
// like 1, 2, 3 etc...
}
});
索引应该相对于单选组,而不是相对于输入元素的整个集合。
组由输入名称(具有相同名称的输入)确定。
最佳答案
要查找radio
在由radio
的name
属性定义的组中的位置:
$('input:radio').each(
function(){
console.log($(this).index('[name="' + this.name + '"]'));
});
引用文献:
关于javascript - 获取当前 radio 元素索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9945862/