前端代码:
<form method="post" action="" onsubmit="set()">
<div class="layui-form-item">
<input type="checkbox" value="1" name="parents" title="1">
<input type="checkbox" value="2" name="parents" title="2">
<input type="checkbox" value="3" name="parents" title="3">
<input type="hidden" name="parent_cat" id="parent" value="">
</div>
<div class="layui-form-item">
<button class="layui-btn" type="submit">下一步</button>
</span>
</div>
</form>
<script>
function set() {
var parents = check_arr('parents');
$('#parent').val(parents);
}
function check_arr($str) {
var str=document.getElementsByName($str);
var objarray=str.length;
var chestr="";
for (i=0;i<objarray;i++)
{
if(str[i].checked == true)
{
chestr+=str[i].value+",";
}
}
return chestr;
}
</script>
后台代码:
$parent_cat = $_POST['parent_cat'];
//删除最后一个逗号(post过来的是"1,2,3,")
//处理之后变为("1,2,3")
$parent_cat = substr($parent_cat,0,strlen($parent_cat)-1);
//然后可以分成数组进行处理
$pros = explode(',',$parent_cat);