简体   繁体   English

在 jquery 数据表中提交表单

[英]Submitting form in the jquery datatable

I have some form checkbox inputs inside jquery datatable.我在 jquery 数据表中有一些表单复选框输入。 When I submit the form, only the inputs that are inside the current page would be submitted.当我提交表单时,只会提交当前页面内的输入。 Please take a look at this code:请看一下这段代码:

<form action="update.php" method="POST" role="form">
 <table id="sampleTable">
  <thead>
    <tr>
      <th style="width: 50%;"> first </th>
      <th style="width: 50%;"> second </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="first[]" value="first" checked/>first</td>
      <td><input type="checkbox" name="second[]" value="second"/>second</td>
    </tr>
  </tbody>
  </table>
 <button type="submit" id="submit-btn">send</button>
 </form>



var table = $('#sampleTable').DataTable({
"pageLength": 2
});

$('#submit-btn').on('click', function(e){
e.preventDefault();
alert($('form').serialize());
});

https://jsfiddle.net/rodinmehr/ba8d1s57/7/ https://jsfiddle.net/rodinmehr/ba8d1s57/7/

As you can see, only two of the checked inputs would be submitted although there are more checked inputs.如您所见,尽管有更多已检查的输入,但只会提交两个已检查的输入。 My assumption is datatable would disable the other pages so it would affect the inputs as well.我的假设是数据表会禁用其他页面,因此它也会影响输入。 Do you have any idea what can be the problem and how can I fix this?你知道什么是问题,我该如何解决这个问题? Any suggestions would be appreciated :)任何建议,将不胜感激 :)

I found a very easy solution for this (may not be the best, but it works!).我为此找到了一个非常简单的解决方案(可能不是最好的,但它有效!)。 When you want to submit the form, just destroy the jquery datatable like this:当你想提交表单时,只需像这样销毁 jquery 数据表:

$('#submit-btn').on('click', function(e){
   table.destroy();
});

After this, your form would be submitted without the datatable, so you would have all the inputs.在此之后,您的表单将在没有数据表的情况下提交,因此您将拥有所有输入。

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

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