sweetAlert preventDefault并返回true [英] sweetAlert preventDefault and return true
问题描述
我尝试了 sweeAlert 插件,该插件效果很好,但是我不知道该怎么做默认值确认后.
I tried sweeAlert plugin, which works perfectly, but I cant figure out how to do default stuff after confirm.
$(document).ready(function () {
function handleDelete(e){
e.preventDefault();
swal({
title: "Are you sure?",
text: "You will not be able to recover the delaer again!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete!",
closeOnConfirm: false
},
function (isConfirm) {
if (isConfirm) {
return true;
}
});
};
});
和按钮
<a href="{plink delete! $row->id_dealers}" class="delete" onclick"handleDelete(event);"> </a>
//{plink delete! $row->id_dealers} Nette -> calls php delete handler
我也尝试了unbind()
和off()
而不是return false
,但没有用.
之前我在onclick
属性中将confirm()
与return true
和return false
一起使用,它可以工作,但是看起来很糟糕.
I also tried unbind()
and off()
instead of return false
, doesnt work.
Earlier I used confirm()
with return true
and return false
in onclick
attribute, it works, but it looks awful.
推荐答案
您可以尝试类似
$(document).ready(function () {
$('.delete').on('click',function(e, data){
if(!data){
handleDelete(e, 1);
}else{
window.location = $(this).attr('href');
}
});
});
function handleDelete(e, stop){
if(stop){
e.preventDefault();
swal({
title: "Are you sure?",
text: "You will not be able to recover the delaer again!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete!",
closeOnConfirm: false
},
function (isConfirm) {
if (isConfirm) {
$('.delete').trigger('click', {});
}
});
}
};
这是一个演示 http://jsbin.com/likoza /1/edit?html,js,输出
Here is a demo http://jsbin.com/likoza/1/edit?html,js,output
另一种方法是使用表格而不是href
.
Another way is the use a form instead of a href
.
标记看起来像这样
<form action="">
<input type="submit" ...... />
</form>
,而不是window.location = $(this).attr('href');
,您只能说form.submit()
如果页面上有多个元素,则可以像这样使用触发器
If there are multiple elements on the page then trigger can be used like this
$(e.target).trigger('click', {});
这是一个演示 http://output.jsbin.com/likoza/2
Here is a demo http://output.jsbin.com/likoza/2
这篇关于sweetAlert preventDefault并返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!