javascript - 怎么能取到选中的checkbox后的label值
PHP中文网
PHP中文网 2017-04-11 12:15:22
[JavaScript讨论组]

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

    确定

  • $("#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后面的值都取到了...

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

    PHP中文网
    PHP中文网

    认证0级讲师

    全部回复(2)
    黄舟

    你的循环里的判断条件写错了,是 $(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);
    })
    阿神

    $("#a1").click(function(){

    var aaa= $(this).prev().find("input").map(function(){return $(this).next().text()}).get().join(","); 
    console.log(aaa);

    })

    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号