1

我正在尝试遍历单选按钮列表以找到匹配的标签并从中构造一个新对象,但是使用此代码我只能得到第一个标签

$target=$();

$radio.each(function(){
    $target = $target.add($source.children('label[for="'+$radio.attr("id")+'"]'));
})
4

1 回答 1

2

$radio.attr("id") 只会获取集合中的第一个元素 id.. 尝试使用.each()$(this)中的或第二个参数 function(index,Element)

$radio.each(function(){
    $target = $target.add($source.children('label[for="'+$(this).attr("id")+'"]'));
})

或者

$radio.each(function(i,v){
    $target = $target.add($source.children('label[for="'+$(v).attr("id")+'"]'));
})

您甚至可以通过做this.idv.id不使用直接访问它.attr('id')

于 2013-04-23T18:04:34.073 回答