我有以下代码:
@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
如何添加确认删除每个数据?
答案 0 :(得分:22)
我更喜欢更简单的方法,只需添加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>
答案 1 :(得分:4)
<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>
答案 2 :(得分:3)
如果这是您的链接:
<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>
需要delete
中的class
。
此解决方案使用Unobtrusive JavaScript,可以使用IE 9或更新版本。
答案 3 :(得分:0)
<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');
答案 4 :(得分:0)
如果这是您的链接:
<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>
需要delete
中的class
。
此解决方案使用Unobtrusive JavaScript,并且应与IE 9或更高版本一起使用。
答案 5 :(得分:0)
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捕获了该错误,并收到了确认警报。