Javascript 在 Laravel 中删除确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32984859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:28:27  来源:igfitidea点击:

delete confirmation in laravel

javascriptphplaravel

提问by yudijohn

I have the following code:

我有以下代码:

@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

How to add a confirmation on delete each data?

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

回答by Mustafa Ehsan Alokozay

I prefer a more easier way, just add onclick="return confirm('Are you sure?')", as bellow:

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

回答by P??

If this is your link:

如果这是您的链接:

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

Use this Javascript:

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

Note:the <a>needs the deletein class. This solution uses Unobtrusive JavaScriptand should work with IE 9 or newer.

注:<a>需要deleteclass。此解决方案使用Unobtrusive JavaScript,应适用于 IE 9 或更新版本。

回答by Anns Rahim

  <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>

回答by Junior Gantin

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

With brexis/laravel-data-methodpackage, you can specify appropriate HTTP method and a confirmation text.

使用brexis/laravel-data-method包,您可以指定适当的 HTTP 方法和确认文本。

This is helpful if you have this for example into your routes file:

例如,如果您将其添加到路由文件中,这将很有帮助:

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

回答by Akash Sethi

If this is your link:

如果这是您的链接:

<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:

路线:

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

Use this Javascript:

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

Note:the <a>needs the deletein class. This solution uses Unobtrusive JavaScriptand should work with IE 9 or newer.

注:<a>需要deleteclass。此解决方案使用Unobtrusive JavaScript,应适用于 IE 9 或更新版本。

回答by francisco armando

index.blade.php

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>

I used pure PHP Form, the method DELETE has to be passed as hidden on the submit page action, so I catch it trough javascript and I get the confirmation alert.

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