php 用laravel形式确认删除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26625584/
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
Confirm delete with laravel form?
提问by David
Problem with confirm delete, all works fine, but if you click cancel in confirm popup it will delete the item from DB. Any ideas? Please show with code example, I'm new to laravel.
确认删除问题,一切正常,但如果您在确认弹出窗口中单击取消,它将从数据库中删除该项目。有任何想法吗?请显示代码示例,我是 laravel 的新手。
<script>
function ConfirmDelete()
{
var x = confirm("Are you sure you want to delete?");
if (x)
return true;
else
return false;
}
</script>
{!! Form::open(['method' => 'DELETE', 'route' => ['path_delete_time', $time->id], 'onsubmit' => 'ConfirmDelete()']) !!}
{!! Form::hidden('case_id', $project->id, ['class' => 'form-control']) !!}
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', array('type' => 'submit', 'class' => 'specialButton')) !!}
{!! Form::close() !!}
回答by lukasgeiter
Just add return to your onsubmit
call. so the value the function returns determines if the form gets submitted or not.
只需将 return 添加到您的onsubmit
通话中即可。所以函数返回的值决定了表单是否被提交。
'onsubmit' => 'return ConfirmDelete()'
回答by Dhaval Tailored
Simple put jQuery code for prompt box when submit form or delete record.
提交表单或删除记录时,简单地将 jQuery 代码用于提示框。
jQuery(document).ready(function($){
$('.deleteGroup').on('submit',function(e){
if(!confirm('Do you want to delete this item?')){
e.preventDefault();
}
});
});
回答by Jeet Thaker
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(".specialButton").click(function(){
return confirm("Do you want to delete this ?");
});
</script>
回答by Hos Mercury
without any function calls ...
没有任何函数调用...
'onsubmit' => 'return confirm("are you sure ?")'
example
例子
{!! Form::open(['url'=>'posts/'.$post->id.'/delete','method'=>'DELETE','class'=>'form-horizontal',
'role'=>'form','onsubmit' => 'return confirm("are you sure ?")'])!!}
using laravel 5
使用 Laravel 5
回答by Laura Chesches
You can give the form an id, and use jQuery:
你可以给表单一个 id,然后使用 jQuery:
{!! Form::open(['method' => 'DELETE', 'route' => ['path_delete_time', $time->id], 'id' => 'FormDeleteTime']) !!}
<script type="text/javascript">
$("#FormDeleteTime").submit(function (event) {
var x = confirm("Are you sure you want to delete?");
if (x) {
return true;
}
else {
event.preventDefault();
return false;
}
});
</script>
回答by Dieter Casier
In my new project (Laravel 5.3) I'm using the following:
在我的新项目(Laravel 5.3)中,我使用了以下内容:
{!! Form::open(['method' => 'DELETE', 'route' => ['admin.user.delete', $user], 'onsubmit' => 'return confirmDelete()']) !!}
<button type="submit" name="button" class="btn btn-default btn-sm">
<i class="fa fa-trash-o"></i>
</button>
{!! Form::close() !!}
Mention the 'onsubmit' => 'return confirmDelete()'otherwise I had some problems when I close the alert without confirming the question.
提及'onsubmit' => 'return confirmDelete()'否则当我在没有确认问题的情况下关闭警报时我会遇到一些问题。
JavaScript:
JavaScript:
function confirmDelete() {
return confirm('Are you sure you want to delete?');
}
回答by khany
Just thought I would generalise this out a bit:
只是想我会概括一下:
.html
.html
<form method="post" action="..." class="has-confirm" data-message="Delete this Thing?">...</form>
.js
.js
$("form.has-confirm").submit(function (e) {
var $message = $(this).data('message');
if(!confirm($message)){
e.preventDefault();
}
});
回答by Daniel Gelling
The only thing that is happening right now is that you get a popup which delays the redirect to the deletion page.
现在唯一发生的事情是你得到一个弹出窗口,延迟重定向到删除页面。
What you might do is only generate a link which on its turn generates a form in js. Like what they're doing here (only with JQuery)
您可能要做的只是生成一个链接,然后在 js 中生成一个表单。就像他们在这里做的一样(仅使用 JQuery)
回答by James Bailey
I know it has been a while, but this is the smoothest way IMO:
我知道已经有一段时间了,但这是 IMO 最顺畅的方式:
'onsubmit' => 'return confirm("Do you want to delete this user?");'
in the form tag.
在表单标签中。