skip to Main Content

I have Table of Books, for each row there’s book no, and book name and delete button which will delete the book
and i am using ajax for deleting, the problem i have when i click the delete button it always delete the first book in the table , any help thanks.

<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>

script

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

2

Answers


  1. you are taking the from by the tag name $(‘form’).serialize() that’s way it take the first form in the table so you can do target.form

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

    and before that console.log(form)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search