Problem with Jquery button

Asked

Viewed 36 times

-2

Guys I have a problem in my code that may be simple to solve but I ended up getting stuck, I’m doing a crud and the add, edit and delete buttons added via Controller in the construction of table cells:

inserir a descrição da imagem aqui

My buttons were repeated per line, that is, for each line of my list of additions in the database, there are the three buttons;

inserir a descrição da imagem aqui

Using Jquery I captured the id of my add button and redirect to the page responsible for insertion in the database, but only my add button from the first line works, the other buttons have no function. Could someone please explain to me what I did wrong? Follow my Jquery code:

inserir a descrição da imagem aqui

1 answer

0

This happens because all "Add" buttons have the same ID. The same happens with the "Delete" and "Edit" buttons. In this case, only the first button is selected and, consequently, the event will only be attached to it. You could simply use classes instead of Ids to select buttons. The code would look like this:

PHP

echo "<td>
    <button type="button" name="add" class="btn btn-success btn-floating add">
        <i class="fa-plus fa"></i>
    </button>
    <button type="button" name="delete" class="btn btn-success btn-floating delete">
        <i class="fa-plus fa"></i>
    </button>
    <button type="button" name="edit" class="btn btn-success btn-floating edit">
        <i class="fa-plus fa"></i>
    </button>
</td>";

Javascript

$('.add').click(function() {
    window.location.href = 'inserepessoa.php';
});
  • Thanks a lot bro, it helped a lot the feedback of you, sometimes I forget the importance of using class instead of id when there are several of the same button, solved my problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.