layui 复选框checked获取值和赋值

取值:

<div class="layui-input-block">
<input type="checkbox" id="permission" name="permission" value="资金管理" title="资金管理">
<input type="checkbox" id="permission" name="permission" value="精准扶贫" title="精准扶贫">
<input type="checkbox" id="permission" name="permission" value="公文审批" title="公文审批">
<input type="checkbox" id="permission" name="permission" value="数据处理" title="数据处理">
</div>
 
 
//读取值信息
console.log(JSON.stringify(GetCheckboxValues("permission")));
 
<script>
//将checke拼接为"value1,value2,value3"
function GetCheckboxValues(Name) {
         var result = [];
         $("[id='" + Name + "']:checkbox").each(function () {
         if ($(this).is(":checked")) {
            result.push($(this).attr("title"));
         }
         });
         return result.join(",");
};
</script>

  

赋值:

						//给复选框赋值	
						var unitType = [];
                        unitType = json.user_permission.split(",");
                        for (var j = 0; j < unitType.length; j++) {
                            var unitTypeCheckbox = $("input[id='permission']");
                            for (var i = 0; i < unitTypeCheckbox.length; i++) {
                                if (unitTypeCheckbox[i].title == unitType[j]) {
                                    unitTypeCheckbox[i].value = unitType[j];
                                    unitTypeCheckbox[i].checked = true;
                                }
                            }
                        }
                        form.render();

  原文链接:https://blog.csdn.net/yugui/article/details/104001884

posted @ 2022-06-26 08:18  迷神图卷  阅读(2047)  评论(0编辑  收藏  举报