<div class="aa">
<input type='radio' id="1" name='choices' value='1'/>
<input type='radio' id ="2" name='choices' value='2'/>
<input type='radio' id="3" name='choices1' value='3'/>
<input type='radio' id="4" name='choices1' value='4'/>
</div>
的jQuery
$(".aa :radio").change(function() {
var names = {};
$('.aa :radio').each(function() { // find unique names
names[$(this).attr('name')] = true;
});
var count = 0;
$.each(names, function() { // then count them
count++;
});
alert ("count " + count + " length " + names.length); //names length comes up as undefined why?
}).change();
答案 0 :(得分:4)
因为names
是object
而不是array
。在您的示例中,count将是对象名称中键的长度。
获取密钥的长度:
var count = 0;
for(var i in names) {
count++;
}
count; // length
或现代浏览器Object.keys(names).length
答案 1 :(得分:0)