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

 傲慢的寒风呼啸_539 发布于 2022-11-10 18:22

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

    确定

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

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

    2 个回答
    • $("#a1").click(function(){

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

      })

      2022-11-12 11:01 回答
    • 你的循环里的判断条件写错了,是 $(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);
      })
      2022-11-12 11:01 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有