tp5和layui表单提交多选复选框checkbox及输入框
未知
2024-04-09 14:45:31
0次
<form class="layui-form layui-form-pane" >
<div class="layui-form-item">
<label class="layui-form-label" >组名称</label>
<div class="layui-input-block">
<input type="text" name="typename" lay-verify="required" class="layui-input" >
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<input type="checkbox" name="purview[]" lay-skin="primary" title="管理员页面" value="admin/index" {php} if(in_array('admin/index',$str_arr))echo 'checked'; {/php}>
<input type="checkbox" name="purview[]" lay-skin="primary" title="添加管理员页面" value="admin/adduser" {php} if(in_array('admin/adduser',$str_arr))echo 'checked'; {/php}>
<input type="checkbox" name="purview[]" lay-skin="primary" title="执行添加管理员" value="admin/insertuser" {php} if(in_array('admin/insertuser',$str_arr))echo 'checked'; {/php}>
</div>
</div>
</form>
第一条办法:
<script type="text/javascript">
layui.use(['form'],function(){
var form=layui.form;
//监听提交事件
form.on('submit(myform)',function(obj){
var purview = [];
$('input[type=checkbox]:checked').each(function() {
purview.push($(this).val());
});
typename=obj.field.typename;
var json = {'purview':purview,'typename':typename};
fields=JSON.stringify(json);
$.ajax({
url:"__MODULE__/admintype/insert",
type:"post",
data:{'val':fields},
beforeSend:function(){
loading=layer.load(1,{shade:[0.4,'#000']})
},success:function(data){
layer.close(loading);
if(data.code==1000){
layer.msg(data.msg,{icon:1})
location.href="__URL__/index";
}else{
layer.msg(data.msg,{icon:2});
}
}
})
return false;
})
})
</script>
第二各办法:
layui.use(['form'],function(){
var form=layui.form;
form.on('submit(myform)',function(obj){
//获取checkbox数据
quotation = new Array();//设置空数组
$("input:checkbox[name='purview']:checked").each(function(){
//数组中追加元素
quotation.push($(this).val());
});
//设置空json
var jsonDe = {};
for (var i = 0; i < quotation.length; i++) {
jsonDe[i] = quotation[i];
}
//把json对象转换成字符串
let myJson = JSON.stringify(jsonDe);
//给obj.field赋值,以便传入后台。注意赋值idmail和html中多选的name=‘purview’。purview键和下方的idemail键会一同传入后台数组。idemail字段在数据库中,purview只是临时用作传值,后台需要删除unset()这个键,否则数据库没有这个字段提示错误。
obj.field.idemail = myJson ;
fields=JSON.stringify(obj.field);
$.ajax({
url:"__URL__/insertMember",
type:"post",
data:{val:fields},
beforeSend:function(){
loading=layer.load(1,{shade:[0.3,'#000']});
},success:function(data){
console.log(data);
// return false;
layer.close(loading);
if(data.code==1000){
layer.msg(data.msg,{icon:1});
location.href="__URL__/index";
}else{
layer.msg(data.msg,{icon:2});
}
}
})
return false;
});
})
后台处理:
if(request()->isPost()){
$request_m=json_decode(htmlspecialchars_decode(input('val')),true);
$request_m['idemail']=implode(',',json_decode($request_m['idemail'],true));
unset($request_m['purview']);
//执行db即可
}
相关内容