Jquery 将更改的复选框值发送到php(数组中)

Jquery 将更改的复选框值发送到php(数组中),jquery,forms,checkbox,Jquery,Forms,Checkbox,当我单击复选框(选中或取消选中)时,我想将其状态(开/关)和id(或名称)的值发送到php(然后发送到sql)。 我的伪代码如下: $("input:checkbox").click(function () { var cat_id = jQuery(this).attr('name'); var status = []; if($("input:checkbox").is(':checked')) { return $(this).

当我单击复选框(选中或取消选中)时,我想将其状态(开/关)和id(或名称)的值发送到php(然后发送到sql)。 我的伪代码如下:

  $("input:checkbox").click(function () {
      var cat_id = jQuery(this).attr('name');
      var status = [];

      if($("input:checkbox").is(':checked')) {
          return $(this).attr();
      } else {
          return $(this).attr();
      }

      $.post("var_dump.php", {
          cat_id: status
      }, // should be id(name of chbox) and its value 1-on, 0-off 
      function (response) {
          $("#result").text(response); //just for checking
      });

      return true;
  });
我将有许多行包含数据,并且每行都有复选框(因此未选中的框将不计算在内),我可以使用
.map()
.each()
来构建数组,但我希望每次更改复选框的状态时都有新值-因此每次都应该是新的
$。post


之所以使用数组,是因为我想用按钮取消选中五个或十个(最后一个)复选框/行,所以它应该是一次性数组。(但这将在以后实现。)

在代码中,return语句甚至不会等待post代码执行。请删除那些不需要的return语句并尝试此操作。还可以将
status
定义为对象而不是数组,因为您希望在post请求中发送键/值对

$("input:checkbox").click(function () {
      var cat_id = jQuery(this).attr('name');
      var status = {};
      //This will get the id or name whatever is available
      status[this.id || this.name] = this.checked ? "1": "0";

      $.post("var_dump.php", {
          cat_id: status
      }, // should be id(name of chbox) and its value 1-on, 0-off 
      function (response) {
          $("#result").text(response); //just for checking
      });

      return true;
  });