其他分享
首页 > 其他分享> > layui 复选框checked获取值和赋值

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

标签:checked,permission,layui,复选框,unitType,result,unitTypeCheckbox,var
来源: https://www.cnblogs.com/CryOnMyShoulder/p/16412917.html