首先判断是否勾选了复选框
1 $("input[name='product']").click(function () {
2 if($(this).prop("checked")==true){
3 alert("点了");
4 }else {
5 alert("取消");
6 }
7
8 })
然后点击添加复选框的值到数组,取消则从数组中把复选框的值拿出来
var id_array=new Array();
$("input[name='product']").click(function () {
if($(this).prop("checked")==true){
id_array.push($(this).val());
alert(id_array);
}else {
removeByValue(id_array,$(this).val());
alert(id_array);
}
})
function removeByValue(arr,val) {
for (var i=0;i<arr.length;i++)
if(arr[i]==val){
arr.splice(i,1);
break;
}
}
最后把所勾选的复选框值数组推送到后台,并且取得页面显示
1 var id_array=new Array();
2 $("input[name='product']").click(function () {
3 if($(this).prop("checked")==true){
4 id_array.push($(this).val());
5 getresult(id_array);
6 }else {
7 removeByValue(id_array,$(this).val());
8 getresult(id_array);
9 }
10
11 })
12
13
14 function removeByValue(arr,val) {
15 for (var i=0;i<arr.length;i++)
16 if(arr[i]==val){
17 arr.splice(i,1);
18 break;
19 }
20 }
21
22
23 function getresult(arr) {
24 $.ajax({
25 url:"{:U('Index/pajax')}",
26 data:{
27 "id_array":arr
28 },
29 type:"post",
30 success:function(result){
31 $("#products").empty();
32 $("#page").empty();
33 $("#products").html(result);
34
35 // alert(result);
36 }
37 })
38 }
来源:https://www.cnblogs.com/raphael1982/p/7865278.html