layui.use(['form', 'jquery'], function() { var form = layui.form, $ = layui.jquery; form.verify({ length6: function(value) { if ((value + "").length > 10) { return "最大长度10位"; } } }); //全局数据数组 var _res = { }; //提示数组 var msg = [{ t: 0, r: [{ t: "顾客订单金额的", u: "%" }, { t: "每单固定理货费用", u: "元" }] }]; //查询 $.ajax({ url: 'costsSetUrl', success: function(res) { _res = res; for (var b = 0; b < res.data.length; b++) { let index = b; msg.map(function(i){ if(i.t == res.data[index].type){ $("#radioText" + res.data[index].type).text(i.r[res.data[index].allotType].t); $("#radioUnit" + res.data[index].type).text(i.r[res.data[index].allotType].u); } }); form.on('submit(formBtn' + res.data[b].type + ')', function(data) { res.data[index] = Object.assign(res.data[index], data.field); return false; }); form.on('radio(radio' + res.data[b].type + ')', function(data) { msg.map(function(i){ if(i.t == res.data[index].type){ $("#radioText" + res.data[index].type).text(i.r[data.value].t); $("#radioUnit" + res.data[index].type).text(i.r[data.value].u); } }); }); for (let v in res.data[b]) { res.data[b][v] += ""; } form.val("form" + res.data[b].type, res.data[b]); } } }); //保存 form.on('submit(formDemo)', function(data) { _res.data.map(function(i) { $('button[lay-filter="formBtn' + i.type + '"]').trigger("click"); }); $.ajax({ url: 'costsSetUrl', type: 'post', contentType: 'application/json', data: JSON.stringify(_res.data), success: function(res) { !(res.code - 0) && layui.layer.msg("保存成功!", { icon: 1 }); } }); return false; }); });