我正在使用甜蜜警报插件来显示警报,如果我点击确定,我想关注我的文本框。
我的问题是:1. 如果我使用timer
属性从甜警报,在jquery的焦点功能不工作2. 但是如果我没有使用property from sweet alert, jquery中的focus函数工作得很好。
也许有人能帮我解决我的问题:D
my script:
swal({
title: "Auto close alert!",
text: "I will close in 2 seconds.",
timer: 2000
});
$('.classMyTextField').focus();
ps:我尝试使用两个焦点功能之间的小功能,但仍然不工作:D
谢谢
您能试试这段代码吗?
swal({
title: "Auto close alert!",
text: "I will close in 2 seconds.",
timer: 2000
}, function(){
swal.close();
$('.classMyTextField').focus();
});
只需添加一个回调函数作为swal()的第二个参数
swal({
title: "Auto close alert!",
text: "I will close in 2 seconds.",
timer: 2000
},
function(){
// SweetAlert confirmed, do something
$('.classMyTextField').focus();
}
);
当SweetAlert 确认或取消:
swal({
title: "Auto close alert!",
text: "I will close in 2 seconds.",
timer: 2000
},
function(isConfirmed){
if(isConfirmed){
swal("Confirmed!", "You confirmed the alert.", "success");
$('.classMyTextField').focus();
}
else{
swal("Cancelled!", "You cancelled the alert.", "warning");
}
}
);
更新:
只显示通知而不显示确认或取消按钮:
swal({
title: "Auto close alert!",
text: "I will close in 2 seconds.",
timer: 2000,
showConfirmButton: false,
showCancelButton: false
},
function(){
// alert closed
$('.classMyTextField').focus();
}
);
做到这一点的最佳和有效的方法是在使用";"关闭swal之前使用.then(function(){
。
使用您的代码,它将是这样的:
swal({
title: "Auto close alert!",
text: "I will close in 2 seconds.",
timer: 2000
}).then(function(){
$('.classMyTextField').focus();
});