Larvel+jquery I want to be able to get the id of the listed data on the console side of js.

Asked 1 years ago, Updated 1 years ago, 291 views

The ultimate goal is to use Ajax to delete the listing button, which is asynchronous and can be removed from the UI.
As a preliminary step, we would like to implement a function that can only be deleted on the front side of the jquery.
In addition, as a way to implement this feature, if you click the button, you will need to get the id of each list on the js side, which is a struggle.

I want to create an html data attribute and get an id on the front side, but I can only get the first id of the list when I click it
How can I get each id by clicking on the button on the list that I got from laravel?If you have any advice, please take care of it.

blade file The process is to get 10 $songs from SongTable.I will omit the code for this variable as the main question is from the front desk.

<table class="table table-striped">
 <thead>
  <tr>
   <th>Title </th>
  </tr>
 </thead>
 <tr>
  @foreach($songs as$song)
  <divid="delete-item">
   <td class="align-middle">
    <a href="{{route('admin.show', ['id'=>$song->id]}}">
     {{ $song->title}}
    </a>
   </td>
   <td class="align-middle">
    // Here it is.
    <button id="delete-btn" data-id="{{$song->id}}" class="btn btn-danger">delete</button>
   </td>
 </tr>
 </div>
 @endforeach
</table>
$(function(){
 $('#delete-btn').on("click", function(){
  console.log($('#delete-btn') .data())
 });
});

jquery laravel ajax

2022-09-30 21:54

1 Answers

How to "add", "delete", "move", and "change" the table lines in JavaScript/jQuery

Using this article as a reference, I was able to delete only the front desk.It seems that you didn't have to specify id(class) individually in the first place.

<button onclick="return confirm('Are you sure you want to delete it?') "class="btn btn-danger removeList" > Delete </button>
$( document ).on('click', '.removeList', function(){
    $(this)
      .parent()
      .parent()
      .remove();
  });


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.