我有以下代码:
@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
如何在删除每个数据时添加确认?
我更喜欢一种更简单的方法,只需添加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>
如果这是您的链接:
<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或更新版本配合使用。
<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>
<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');
如果这是您的链接:
<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或更新版本配合使用。
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捕获它,并得到确认警报。
<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>
正确的路线方法