【问题标题】:Adding jQuery objects to an empty one将 jQuery 对象添加到空对象
【发布时间】:2013-04-17 01:32:35
【问题描述】:

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

$target=$();

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

【问题讨论】:

    标签: jquery arrays object collections add


    【解决方案1】:

    $radio.attr("id") 只会获取集合中的第一个元素 id。尝试使用$(this).each() 中的第二个参数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')

    【讨论】:

    • 谢谢,成功了!.. 也感谢 this.id 的建议。 ;)
    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2014-09-14
    • 2022-01-13
    • 2010-12-17
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多