Jquery 在用户单击“确定”后才发出甜警报
我正在将SweetAlert与Symfony一起使用,我希望用户在完成删除操作之前进行确认 当用户单击删除按钮时,会弹出SweetAlert,然后立即消失,项目被删除 在用户从SweetAlert弹出窗口单击ok之前,我不希望删除该项目 这是我的设置 JS: HTML:Jquery 在用户单击“确定”后才发出甜警报,jquery,symfony,sweetalert,Jquery,Symfony,Sweetalert,我正在将SweetAlert与Symfony一起使用,我希望用户在完成删除操作之前进行确认 当用户单击删除按钮时,会弹出SweetAlert,然后立即消失,项目被删除 在用户从SweetAlert弹出窗口单击ok之前,我不希望删除该项目 这是我的设置 JS: HTML: 更简单的解决方案是直接在onclick中添加函数,如下所示: <a href="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}" onclick=
更简单的解决方案是直接在
onclick
中添加函数,如下所示:
<a href="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}"
onclick="return confirm('are u sure?')" class="btn btn-white"
data-toggle="tooltip" data-toggle="tooltip" data-placement="top" title="Delete">
<i class="fa fa-trash"></i>
</a>
有什么不同吗?您可以按以下操作:
<a href="/delete/me" class="confirm" data-title="Want to delete this?" data-type="warning">Click me</a>
<a href="/edit/me" class="confirm" data-title="Info image" data-type="info">Click me</a>
要等待用户确认,您必须使用e.preventDefault()取消链接操作代码>
然后在用户确认后手动转到链接。您可以像这样在控制器中设置警报
public function index() //normal index
{
return view('hello.index');
}
public function index_alert($message) //index with alert
{
return redirect()->route('hello.index')->with('success', $message);;
}
并将ajax/javascript设置为“警报”路径
并在web.php中添加此路由
Route::get('index/alert/{message}', 'YourController@index_alert');
这和Symfony无关。您必须防止默认浏览器行为(返回false onclick,即),因此我将onclick更改为:myFunction();返回false代码>并且它现在没有删除任何内容。我可以单击“确定”,但它不会删除。您在哪里读到的sweetalert允许您删除任何内容?它只是一个简单的js警报函数,看起来很漂亮。您可以将ajax调用添加到isConfirm
函数中,以删除Symfony后端中的操作。@malcolm我在使用ajax方面也有问题。删除按钮来自一个php foreach,由于某些原因,它不会从我单击的锚返回正确的URL。js对php foreach没有任何内容,它读取生成的html。一定有原因,你必须找到它:)是的,这对我来说很有效,但我使用的是SweetAlert。我不想使用标准的JS警报。我是新手。我明白你为什么要用它了。尝试我添加的编辑2更改。仍然会立即删除并立即关闭弹出窗口。顺便说一句,谢谢你的帮助。你是否使用浏览器工具设置了javascript断点并单步执行代码,以查看它是否真正到达了该函数,以及到底发生了什么?我怀疑是别的东西!
function myFunction() {
swal({
title: "Are you sure?",
text: "You will not be able to recover this Job Title!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function () {
swal("Deleted!", "Job Title has been deleted.", "success");
});
};
<a href="/delete/me" class="confirm" data-title="Want to delete this?" data-type="warning">Click me</a>
<a href="/edit/me" class="confirm" data-title="Info image" data-type="info">Click me</a>
$(function() {
$('a.confirm').click(function (e) {
e.preventDefault();
var tthis = $(this);
swal({
title: "Are you sure?",
text: $(this).data('title'),
type: $(this).data('type'),
showCancelButton: true,
confirmButtonText: "Yes",
cancelButtonText: "No",
}, function (isConfirm) {
if (isConfirm) {
document.location.href = tthis.attr('href');
}
});
});
});
public function index() //normal index
{
return view('hello.index');
}
public function index_alert($message) //index with alert
{
return redirect()->route('hello.index')->with('success', $message);;
}
$.ajax({
...
success:function(data){
window.location.href = "index/alert/"+data.message;
}
});
Route::get('index/alert/{message}', 'YourController@index_alert');