HTML
<form method="post" action="">
<input type="checkbox" name="checkbox" value="1">1
<input type="checkbox" name="checkbox" value="2">2
<input type="checkbox" name="checkbox" value="3">3
<input type="checkbox" name="checkbox" value="4">4
<input type="button" value="添加" onClick="addRegion()">
</form>
button按钮调用addRegion方法
function addRegion(){
var str="";
$("input[name='checkbox']:checkbox").each(function(){
if($(this).is(":checked")){
str += $(this).val()+","
}
})
str.split(",");
if(str==""){
str = '您还没有选择';
alert(str);
return false;
} else {
}
$.ajax(
{
type:"POST",
data:{regional:str},
url:'/Item/addRegionalModels',
dataType:"json",
success:function(msg)
{
if(msg.ret == 1){
modalAlert('提示','添加成功');
}else{
modalAlert('提示','添加失败');
}
}
}
)
}
控制器/Item/addRegionalModels
public function addRegionalModels(){
$data['regional'] =$_POST['regional'];
$newstrs = substr($data['regional'],0,strlen($data['regional'])-1);
$arr = explode(",",$newstrs);
foreach($arr as $u){
$strarr = explode(",",$u);
foreach($strarr as $newstr){
$sql = "INSERT into test(brand) VALUES('".$newstr."')";
$list = $Model->query($sql);
}
}
}