在 laravel 中删除确认

2022-08-30 17:56:49

我有以下代码:

@foreach($results as $result)
  <tr>
    <td>{{$result->my_id}}</td>
    <td>{{$result->province_name}}</td>
    <td>{{$result->city_name}}</td>
    <td>
      <a class="btn btn-primary" href="{{route('city-edit', $result->my_id)}}"><i class="fa fa-edit"></i></a>
      <a class="btn btn-danger" href="{{route('city-delete', $result->my_id)}}"><i class="fa fa-trash"></i></a>
    </td>
  </tr>
@endforeach

如何添加删除每个数据的确认?


答案 1

我更喜欢更简单的方法,只需添加,作为波纹管:onclick="return confirm('Are you sure?')"

<a class="btn btn-danger" onclick="return confirm('Are you sure?')" href="{{route('city-delete', $result->my_id)}}"><i class="fa fa-trash"></i></a>

答案 2

如果这是您的链接:

<a href="#" class="delete" data-confirm="Are you sure to delete this item?">Delete</a>

使用此 Javascript:

var deleteLinks = document.querySelectorAll('.delete');

for (var i = 0; i < deleteLinks.length; i++) {
    deleteLinks[i].addEventListener('click', function(event) {
        event.preventDefault();

        var choice = confirm(this.getAttribute('data-confirm'));

        if (choice) {
            window.location.href = this.getAttribute('href');
        }
    });
}

注意:需要 in .此解决方案使用不显眼的 JavaScript,应该适用于 IE 9 或更高版本。<a>deleteclass