Laravel:关于点击功能问题的jQuery

我正在尝试alert,但是单击功能是第一个按钮上的running only once。但我有buttons on many rows

我正在通过Laravel从表中的数据库中获取数据。只有一个按钮可以运行一项功能,其他按钮则什么也没有。

jQuery:

jQuery(document).ready(function(e)  {
    $('#restore-data').on('click',function(e) {
    var val = $("#thisr").attr('value');
    alert(val);

    });
});

查看:

<table id="recover-table" class="table" >
    <thead>
    <tr class="bg-info">
        <th>Teacher Name</th>
        <th>Date</th>
        <th>action</th>
    </tr>
    </thead>
    <tbody>

    @foreach($trashs as $trash)

    <tr id="thisr" value="{{$trash->id}}">
    <td class="text-nowrap ">{{$trash->efirst}} {{$trash->esecond}}</td>

    <td class="text-nowrap ">{{$trash->deleted_at}}</td>

    <td class="text-nowrap "><button type="submit"  class="" name="id" 
    value="{{$trash->id}}" id="restore-data">Delete</button></td>

    </tr>

    @endforeach </tbody></table>

现在,即使警报也无法正常工作,但我想在从表中删除记录后实现刷新表。

更新:现在,警报工作正常,但是当我通过按下按钮删除记录时,仅删除了一行。该功能运行一次。

完整的Js:

jQuery(document).ready(function(e)  {

    $('#restore-data').on('click',function(e) {

    let teacher_id=$(this).attr('value');
    console.log('Restore button clicked!')
    e.preventDefault();

    $.ajax(
    {
      url: "/teachers/recover/" + $('#restore-data').attr("value"),type: 'GET',// Just delete Latter Capital Is Working Fine
      headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },data: teacher_id,success: function (data) {
     console.log(data.msg);
     console.log(teacher_id);

     if(data.msg){
         $('#thisr').remove();
         $('#response').empty();
         $(".toast").toast('show');
         $('#response').append(data.msg);
      }
    },error: function (xhr) {
    console.log("Error Restoring Record");
    //console.log(xhr.responseText);
    },});
  });
});
superlinkai 回答:Laravel:关于点击功能问题的jQuery

更改此行。

var val = $("#thisr").attr('value');

到(因为您在按钮中具有值属性):

var val = $(this).attr('value');

或(因为您具有值属性td):

var val = $(this).parent('tr#thisr').attr('value')

要删除行。

$('#restore-data').on('click',function(e) {

 var _this = $(this);
 ...

if(data.msg){
  _this.parent('tr#thisr').remove();
....

还将按钮类型更改为按钮。

<button type="button"  class="" name="id" 
    value="{{$trash->id}}" id="restore-data">Delete</button></td>
,

您可以尝试使用“恢复数据”类

$(document).ready(function(e)  {
    $(document).on('click','.restore-data',function(e) {
        var val = $('#thisr').val();
        alert(val);
    });
});

每个元素的ID应该是唯一的。

您可以尝试

@foreach($trashs as $trash)

<tr>
<td class="text-nowrap ">{{$trash->efirst}} {{$trash->esecond}}</td>

<td class="text-nowrap ">{{$trash->deleted_at}}</td>

<td class="text-nowrap ">
    <button type="button" data-trash-id="{{$trash->id}}" class="restore-data">Delete</button>
</td>

</tr>

@endforeach

和JS

$(document).on('click',function(e) {
    var trash_id = $(this).attr('data-trash-id');
    alert(trash_id);
});
,

您为所有按钮赋予了相同的id,并且id必须与特定按钮唯一,因此您可以使用array键定义唯一的id并将其传递给Function

@foreach($trashs as $key=>$trash)

<tr id="thisr{{$key}}">
    <td class="text-nowrap ">{{$trash->efirst}} {{$trash->esecond}}</td>
    <td class="text-nowrap ">{{$trash->deleted_at}}</td>
    <td class="text-nowrap ">
        <button type="button"  class="" name="id" value="{{$trash->id}}" id="restore-data{{$key}}" onclick="restoreData({{$key}})">Delete</button>
    </td>
</tr>
@endforeach
function restoreData(key){
    var val = $("#restore-data"+key).attr('value');
    alert(val);
    // you can use either
    $("#restore-data"+key).closest('tr').remove();
    // OR
    $('#thisr'+key).remove();
}
本文链接:https://www.f2er.com/3154680.html

大家都在问