jQuery 利用通配符获取多选框选中的值

1.首先看看jQuery的通配符使用:

$("input[id^='code']");//id属性以code开始的所有input标签

$("input[id$='code']");//id属性以code结束的所有input标签

$("input[id*='code']");//id属性包含code的所有input标签

参考:https://www.cnblogs.com/gilbert/p/5358145.html

2.然后结合选中属性选中所有节点:

$("input[id^='productTypeId_']:checked")

3.通过each函数拼接字符串完成获取值的操作

 function getCheckBoxVal() {
      var str = "";
      $("input[id^='productTypeId_']:checked").each(function (index, item) {
             str += productTypeListForJS[$(this).val()] + ",";
       });
            return str;
}
参考:https://www.cnblogs.com/youmingkuang/p/10000856.html

4.结合点击事件来获取值

$("input[id^='productTypeId_']").click(function () {
            // relatedTips();
            var checkBoxVal = getCheckBoxVal();
            console.log(checkBoxVal);
        })
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!