【问题标题】:Send changed value of checkbox to php ( in array )将复选框的更改值发送到 php(在数组中)
【发布时间】:2012-03-05 02:33:07
【问题描述】:

当我单击复选框(选中或取消选中)时,我想将其状态(开/关)和 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 值。

数组的原因是我想制作按钮来取消选中五个或十个(最后一个)复选框/行,所以它应该是一个时间数组。 (但这将在以后实施。)

【问题讨论】:

    标签: jquery forms checkbox


    【解决方案1】:

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

    $("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;
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2015-09-13
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多