我正在使用swal(http://t4t5.github.io/sweetalert)从用户单击某些内容时获得一些数据。然后,我想从调用swal的函数中返回它。换句话说,对于下面的示例,我想将labels
中项目的文本设置为输入值。问题是它似乎在关闭swal /输入数据之前就返回了:
.on('dblclick', function(l) {
labels.text(function (e) {
return swal({
title: "Edit label"
},
function(inputValue){
if(inputValue) {
return inputValue
}
});
})
});
正常的
prompt alert
处于阻止状态,因此可以执行此操作,swal
可以这样做吗?谢谢
最佳答案
虽然您无法获得Sweet Alerts阻止,但是您可以使用其回调功能来触发所有需要先解除警报的代码。示例中有一些实例。例如,假设您有一个是/否样式警报,则有一个文件删除示例。
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm){
//The callback will only fire on cancel if the callback function accepts an
//argument. So, if the first line were 'function () {' with no argument, clicking
//cancel would not fire the callback.
if (isConfirm) {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
或AJAX示例:
swal({
title: "Ajax request example",
text: "Submit to run ajax request",
type: "info",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
},
function(){
setTimeout(function(){
swal("Ajax request finished!");
}, 2000);
});
在这两种方法中,直到警报与之交互,才会触发回调,并且将调用的结果作为回调的参数传入。
因此,说您需要等到有人单击“确定”。
swal({
title: "Delete your account?",
text: "Clicking on continue will permanently delete your account.",
type: "warning",
confirmButtonText: "Continue",
closeOnConfirm: false
}, function () {
swal("Deleted account", "We'll miss you!", "success");
});
注意:仅当您在回调中显示后续警报时,
closeOnConfirm
/ closeOnCancel
仅需要为false
。如果将其设置为true
,它将在显示给用户之前关闭第二个警报。但是,如果您执行的是与swal
不相关的操作,并且没有关闭它,则它将无限期保持打开状态。swal({
title: "Delete your account?",
text: "Clicking on continue will permanently delete your account.",
type: "warning",
confirmButtonText: "Continue"
}, function () {
console.log("This still shows!")
});
如果要在执行与
swal
不相关的操作时使警报保持打开状态,则应在代码末尾调用swal.close()
。swal({
title: "Delete your account?",
text: "Clicking on continue will permanently delete your account.",
type: "warning",
confirmButtonText: "Continue",
closeOnConfirm: false
}, function () {
console.log("This still shows!");
setTimeout(function () {
// This will close the alert 500ms after the callback fires.
swal.close();
}, 500);
});
关于javascript - Sweetalert像正常提示一样阻止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32980981/