🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 表单提交数组 ## 12.4.表单提交数组 layui的表单提交、赋值等不支持数组及嵌套的格式,可以通过简单的格式处理数据为自己想要的格式: ~~~ <form class="layui-form" lay-filter="demoForm"> <input name="name" class="layui-input"/> <input name="role__0" class="layui-input"/> <input name="role__1" class="layui-input"/> <input name="role__2" class="layui-input"/> <input name="sex__0" class="layui-input"/> <input name="sex__1" class="layui-input"/> <button class="layui-btn" lay-filter="demoSubmit" lay-submit>提交</button> </form> <script> /* 监听表单提交 */ form.on('submit(demoSubmit)', function (data) { var roleList = [], sexList = []; for(var f in data.field) { if(f.indexOf('role__') === 0) { roleList.push(data.field[f]); delete data.field[f]; } if(f.indexOf('sex__') === 0) { sexList.push(data.field[f]); delete data.field[f]; } } data.field.roleList = roleList; data.field.sexList = sexList; console.log(data.field); return false; }); /* 赋值 */ var res = {roleList:[], sexList: []}; for(var i=0;i<res.roleList.length;i++) { res['role__'+i] = res.roleList[i]; } form.val('demoForm', res); </script> ~~~