Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

怎么能取到选中的checkbox后的label值

要实现的效果是选中一级类目或者选中二级类目点确定按钮,能取到所选的值.

图片描述

<li>
<label><input type="checkbox" value="" class="ab">家电</label>
<div class="dusInfo">
<ol class="clearfix">
<li><input type="checkbox" value="050101"><label>空调</label></li>
<li><input type="checkbox" value="050102"><label>厨卫</label></li>
<li><input type="checkbox" value="050103"><label>冰洗</label></li>
<li><input type="checkbox" value="050104"><label>小家电</label></li>
<li><input type="checkbox" value="050105"><label>TV</label></li>
</ol>
<a href="#" id="a1">确定</a>
</div>
</li>
$("#a1").click(function(){
var iii = $(this).prev().find("input");
var aaa = '';
$(iii).each(function(){
if( $(iii).prop("checked") == true ){
aaa += $(this).next().text()+',';
}else{
console.log("1")
}
})
console.log(aaa);
})

现在我这个方法就是点击确认按钮 把所有的checkbox后面的值都取到了...

求各位帮忙看看问题出在哪了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

你的循环里的判断条件写错了,是 $(this) 不是 $(iii)

$("#a1").click(function(){
    var iii = $(this).prev().find("input");
    var aaa = '';
    $(iii).each(function(){
        if( $(this).prop("checked") == true ) {
            aaa += $(this).next().text()+',';
        } else {
            console.log("1")
        }
    })
    console.log(aaa);
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...