Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何一次选择多个复选框?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何一次选择多个复选框?

Javascript 如何一次选择多个复选框?,javascript,jquery,html,Javascript,Jquery,Html,我有一个动态创建的html表。我正在为每行添加复选框 $.each(data, function(id, value) { rows.push('<tr><td><input type="checkbox" autocomplete="off" class="chkbox" name="chkbox" value="'+value.site+':'+value.client+'"></td><td>' + id + '</t

我有一个动态创建的html表。我正在为每行添加复选框

$.each(data, function(id, value) {
  rows.push('<tr><td><input type="checkbox"  autocomplete="off" class="chkbox" name="chkbox" value="'+value.site+':'+value.client+'"></td><td>' + id + '</td><td>' + value.machine + '</td><td>' + value.state + '</td><td>' + value.date_processed + '</td><td><button type="button" onclick="resetSite(\''+ value.site+'\',\''+value.client+'\')">Reset</td></tr>');
});
在上述功能中,如果我选择了多行,我的警报框会多次显示同一站点。
我如何克服这个问题?

改变

$(document).on('change','.chkbox',function () {
  var sites = [];
  var value = $(this).val();
  var res = value.split(":");
  $.each($("input[class='chkbox']:checked"), function(){            
    sites.push(res[0]);
  });
  alert("sites are: " + sites.join(", "));
  $.ajax({
    type: "POST",
    url: "http://localhost:8080/cache/setProcessing?site="+res[0]+"&client="+res[1],
    /*data: {
      chkbox: value 
    },*/
    error : function(xhr, textStatus, errorThrown) {
      alert("Failed. Error : " + errorThrown);
    }
  });
});
cor·re·respond·ing

$(document).on('change','.chkbox',function () {
  var sites = [];
  $.each($("input[class='chkbox']:checked"), function(){    
    var value = $(this).val(); // must me be inside the each
    var res = value.split(":");   // must me be inside the each     
    sites.push(res[0]);
  });
  alert("sites are: " + sites.join(", "));
  $.ajax({
    type: "POST",
    url: "http://localhost:8080/cache/setProcessing?site="+res[0]+"&client="+res[1],
    /*data: {
      chkbox: value 
    },*/
    error : function(xhr, textStatus, errorThrown) {
      alert("Failed. Error : " + errorThrown);
    }
  });
});
因为在前面的代码中,它只获取您单击的当前复选框的值


因此,您必须将
res
放在
每个函数中
才能知道所有已选中的复选框,并获得相应的值。

我认为问题在于此
站点。推送(res[0])所以它只会一直推送
res[0]
的值。这是什么res[0]?所以你总是一直推同一个值time@ShadowFiend否,res包含“+value.site+”:“+value.client”的联接值。所以我拆分并得到第一个数组[0]value@Se0ng11否,res包含“+value.site+”:“+value.client”的联接值。因此,我拆分并获得第一个数组[0]值–@Ratha您在内部尝试过这两种方法吗?不仅仅是值,还有每个函数中的变量res?类似于
$.each($($(输入[class='chkbox']:选中),function(){var value=$(this.val();var res=value.split(“:”);sites.push(res[0]);})这里如何更改Ajax调用?现在它每次都调用后端。我想在复选框进程完成后调用它over@Ratha你所说的复选框过程结束是什么意思?对不起,我的不好..这符合我的要求。我想问,在选择了多行之后,我想将该站点[]发布到后端服务。您能解释清楚吗?如果用户完成了复选框的检查,那么调用ajax?像那样?是的…这就是我想要的…也许我可以放个按钮什么的?
$(document).on('change','.chkbox',function () {
  var sites = [];
  $.each($("input[class='chkbox']:checked"), function(){    
    var value = $(this).val(); // must me be inside the each
    var res = value.split(":");   // must me be inside the each     
    sites.push(res[0]);
  });
  alert("sites are: " + sites.join(", "));
  $.ajax({
    type: "POST",
    url: "http://localhost:8080/cache/setProcessing?site="+res[0]+"&client="+res[1],
    /*data: {
      chkbox: value 
    },*/
    error : function(xhr, textStatus, errorThrown) {
      alert("Failed. Error : " + errorThrown);
    }
  });
});