前端页面
1 <form class="layui-form layui-form-pane" lay-filter="formAdd" id='formAdd'> 2 <div class="layui-form-item"> 3 <label class="layui-form-label">类别</label> 4 <div class="layui-input-block"> 5 <input type="radio" name="category" value="类别1" title="类别1" checked /> 6 <input type="radio" name="category" value="类别2" title="类别2" /> 7 </div> 8 </div> 9 <div class="layui-form-item"> 10 <div class="layui-input-block"> 11 <button class="layui-btn" lay-submit lay-filter="addform">确定添加</button> 12 </div> 13 </div> 14 </form>
JS代码
1 layui.use(['form'],function(){ 2 var form = layui.form 3 , layer = layui.layer; 4 form.render(); 5 form.on('submit(addform)', function (data) { 6 $("form#formAdd input[type='radio']:checked").each(function(){ 7 data.field[$(this).attr('name')] = $(this).attr('title') 8 }); 9 $.ajax({ 10 type: "GET", 11 contentType: "application/json; charset=utf-8", 12 url: '/url_str/', 13 data: data.field, 14 dataType: 'json', 15 success: function (result) { 16 if (result.flag == 1) { 17 layer.msg("记录添加成功"); 18 } else { 19 layer.alert(result.message); 20 } 21 }, 22 error: function (XMLHttpRequest, textStatus, errorThrown) { 23 console.log(XMLHttpRequest.status); 24 console.log(XMLHttpRequest.readyState); 25 console.log(textStatus); 26 } 27 }); 28 return false; 29 }); 30 });
来源:https://www.cnblogs.com/rn-05181226-rw/p/12420688.html