【问题标题】:Serialize the specific form in Ajax request POST序列化ajax请求POST中的具体表单
【发布时间】:2020-12-31 07:53:58
【问题描述】:

我有书目表,每一行都有书号,书名和删除按钮,将删除该书 我正在使用ajax进行删除,当我点击删除按钮时我遇到的问题总是删除表中的第一本书,任何帮助谢谢。

<table>
 <thead>
  <tr>
   <th>Book NO</th>
   <th>Book Name</th>
   <th>Remove</th>
  </tr>
 </thead>
 <tbody>
  {% for book in book_List %}
   <tr>
    <td>{{book.no}}</td>
    <td>{{book.name}}</td>
    <td>
     <form method="POST">
      <input type="hidden" name="remove_uuid" value="{{ book.uuid}}">
      <button onclick="delete()">Delete</button>

     </form>

    </td>
  </tr>
 {%endfor%}
 </tbody>
</table>

脚本

function delet(){
     $.ajax({
     type:'POST',
     data: $('form').serialize(),
     success: function (data) {
       alert("the book has been delete it")
     }
}

【问题讨论】:

    标签: jquery django ajax


    【解决方案1】:

    您正在通过标签名称 $('form').serialize() 获取 from,这就是它采用表中第一个表单的方式,因此您可以执行 target.form

    function delet(){
     form = event.target.form;
     $.ajax({
      type:'POST',
      data: $(form).serialize(), 
      success: function (data) {
       alert("the book has been delete it")
     }
    }
    

    【讨论】:

      【解决方案2】:

      使用$(form).serialize(),

      在 console.log(form) 之前

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 2020-09-23
        • 2019-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多