gpt4 book ai didi

javascript - 像普通提示一样阻止 sweetalert

转载 作者:行者123 更新时间:2023-11-30 12:13:51 26 4
gpt4 key购买 nike

我正在使用 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 block ,但您可以使用它的回调功能来触发任何需要首先解除警报的代码。这些例子有几个这样的例子。例如,假设您有一个是/否样式的警报,这就是文件删除示例。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com