laravel 单个按钮上的 HTML DELETE 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30745507/
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
HTML DELETE Method on a single button
提问by cheese5505
I'm using Laravel with a Route::resource()
controller and to delete something it needs to run the function destroy()
. This method requires a DELETE HTTP method to go off. How do I do this on a single button?
我正在将 Laravel 与Route::resource()
控制器一起使用,并删除运行该功能所需的内容destroy()
。此方法需要 DELETE HTTP 方法才能关闭。如何在单个按钮上执行此操作?
Thanks
谢谢
采纳答案by karanmhatre
You can create a form around the delete button. This will not add anything to the page visually.
您可以围绕删除按钮创建一个表单。这不会在视觉上向页面添加任何内容。
For example:
例如:
{{ Form::open(['url' => 'foo/bar', 'method' => 'delete', 'class' => 'deleteForm']) }}
<input type="submit" class="deleteBtn" />
{{ Form::close() }}
The Laravel Form helper automatically spoofs the form and adds the hidden field for the DELETE
method.
Laravel Form 助手会自动欺骗表单并为DELETE
方法添加隐藏字段。
Then you can style the button using the .deleteBtn
class. If the button needs to be positioned inline, you can even assign a display: inline;
property to the .deleteForm
class.
然后,您可以使用.deleteBtn
该类来设置按钮的样式。如果按钮需要内联定位,您甚至display: inline;
可以为.deleteForm
该类分配一个属性。
回答by chanafdo
You could add a form and use Laravel's Form Method Spoofing
您可以添加一个表单并使用 Laravel 的表单方法欺骗
<input type="hidden" name="_method" value="DELETE">
or you could use ajax (Example code below uses jQuery)
或者你可以使用 ajax (下面的示例代码使用 jQuery)
$.ajax({
url: 'YOUR_URL',
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
回答by Juancho Ramone
You could also use this library https://gist.github.com/soufianeEL/3f8483f0f3dc9e3ec5d9to implement this:
你也可以使用这个库https://gist.github.com/soufianeEL/3f8483f0f3dc9e3ec5d9来实现这个:
<a href="posts/2" data-method="delete" data-token="{{csrf_token()}}" data-confirm="Are you sure?">