我正在使用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
可以这样做吗?
由于
答案 0 :(得分:2)
虽然您无法使用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);
});
答案 1 :(得分:1)
不,它无法创建等待用户输入的阻止代码。只有JS已经提供的方式:提示,警报等。只需从回调中运行所需的代码。