简体   繁体   English

选中复选框的行表中的表单中没有复选框数据

[英]No checkbox data in form from a row table where checkbox is selected

In a table, each row contains a checkbox in an unique form like this: 在表格中,每一行都包含一个独特形式的复选框,如下所示:

<td class="d-inline-block col-2">
        <div class="form-check">
          <form action="/slider-update/1" method="post" id="check-1">
            <input type="hidden" name="csrfmiddlewaretoken" value="uDIiSPShbE3R2COQDI0qE4SlJGiqsQy8CQ2nQ0jllibaOqH6YsU8TtL7Xy0pF8sI">
            <input class="form-check-input position-static" type="checkbox" id="1" value="is_active" aria-label="...">
          </form>
        </div>
 </td>

Each form is identified with an id formatted with a pk (here: "check-1"). 每个表单都带有一个以pk格式格式化的ID(此处为“ check-1”)。 Despite all these clear informations, I can't get my form! 尽管有所有这些明确的信息,但我无法获得表格!

Here is my javascript code: 这是我的JavaScript代码:

    $(document).on('click', 'form', function () {
      var rpk = $(this).attr("id");
      var url = $(this).attr("action");
      var form = $('#' + rpk);
      $.post(url, form.serializeArray())
          .done(function (data) {
              alert( data );
          });
  }); 

I get the correct form but it contains only the token, nothing related to the checkbox. 我得到了正确的表格,但它只包含令牌,与复选框没有任何关系。

You need to set name attribute to your checkbox. 您需要将name属性设置为您的复选框。 jQuery document here serializeArray jQuery文档在这里serializeArray

that's change your code like this: 这将更改您的代码,如下所示:

 <input class="form-check-input position-static" type="checkbox" id="1" name= "checkbox-1" value="is_active" aria-label="...">

also in your JavaScript code. 也在您的JavaScript代码中。 Don't need to do 不需要做

var form = $('#' + rpk);

you can just use 你可以用

$(this).serializeArray()

that's : 那是 :

    $(document).on('click', 'form', function () {
      var url = $(this).attr("action");
      $.post(url, $(this).serializeArray())
          .done(function (data) {
              alert( data );
          });
  }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM