javascript 在laravel中删除确认

nqwrtyyt  于 2022-11-27  发布在  Java
关注(0)|答案(7)|浏览(150)

下面的代码:

@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

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

g9icjywg

g9icjywg1#

我更喜欢更简单的方法,只需添加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>
roejwanj

roejwanj2#

如果这是您的链接:

<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');
        }
    });
}

注意:<a>需要class中得delete.此解决方案使用Unobtrusive JavaScript,应该与IE 9或更高版本一起使用.

q0qdq0h2

q0qdq0h23#

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

<script>
  function myFunction() {
      if(!confirm("Are You Sure to delete this"))
      event.preventDefault();
  }
 </script>
6bc51xsx

6bc51xsx4#

<a href="{{ route('city-delete', $result->my_id) }}" 
   class="btn btn-danger" data-method="DELETE" data-confirm="Are you sure?"> Delete</a>

使用brexis/laravel-data-method包,可以指定适当的HTTP方法和确认文本。
例如,如果您在路由文件中包含以下内容,这将非常有用:

Route::get('cities/{city}', 'CityController@show')->name('city-show');
Route::delete('cities/{city}', 'CityController@destroy')->name('city-delete');
ccrfmcuu

ccrfmcuu5#

如果这是您的链接:

<a href="{{route('venuepropertyarea.delete', ['propertyarea' => $propertyareaname->id])}}" data-method="DELETE" data-confirm="Are you sure to delete this item?" class="btn btn-danger btn-xs pull-right delete"><i class="fa fa-trash"></i> </a>

给药途径:

Route::get('/icrm/venues/property_area/delete/{propertyarea}', 'VenuePropertyAreaController@deletepropertyarea')->name('venuepropertyarea.delete');

使用此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');
        }
    });
}

注意:<a>需要class中得delete.此解决方案使用Unobtrusive JavaScript,应该与IE 9或更高版本一起使用.

mitkmikd

mitkmikd6#

index.blade.php

<form action="{{route('todos.destroy', $todo->Id)}}" method="post">
              <input type="hidden" name="_method" value="DELETE">
              @csrf
      <button id="btnDelete"class="btn btn-danger btn-sm">Delete</button>
     </form>
<script type="javascript">
       document.onsubmit=function(){
           return confirm('Sure?');
       }
</script>

我使用的是纯PHP表单,DELETE方法必须在提交页面操作中隐藏传递,所以我通过javascript捕获它,并得到确认警报。

w80xi6nr

w80xi6nr7#

<a  class="btn btn-danger px-5  radius-30" onclick="return confirm('Are you sure you want to Unassign this agent?')" href="{{ route('unassign.agent', $report->id) }}" >Unassign</a>

正确路由方法

相关问题