我已经被困在这几天了。我正在使用sweetalert2,我想要完成的是根据给出的答案触发javascript函数,所以如果我单击OK它会触发一件事,如果我点击取消它会做其他事情。我可以让它工作,但它似乎在甜蜜警报完成之前触发功能。以下是代码示例:
<script>
function validateSubmit(a,b,c){
var mode = a;
var info = b;
var trunk = c;
var iteration = baseName(info);
if (mode === 'Update' && info != trunk ){
confirmGetMessage(iteration);
}
else {
alert('seems to work')
}
}
function baseName(str) {
var base = new String(str).substring(str.lastIndexOf('/') + 1);
if(base.lastIndexOf(".") != -1)
base = base.substring(0, base.lastIndexOf("."));
return base;
}
function trythis(){
alert('made it right here!');
}
function confirmGetMessage(info){
var message = "<h3>" + info + "</h3><br/>Or Revert Back to Trunk?"; var contmessage = "Updating " + info;
swal({
title: "Update Branch?",
html: message,
type: "question",
showCancelButton: true,
cancelButtonText: "Switch To Trunk",
cancelButtonColor: "#0080FF",
confirmButtonColor: "#DD6B55",
confirmButtonText: "Continue Update",
closeOnConfirm: false,
closeOnCancel: false
}).then(
function(result){
swal({
text: contmessage,
timer: 1400,
showConfirmButton: false
}), alert('work please');
}, function(dismiss) {
swal({
text: 'Switching to Trunk',
timer: 1400,
showConfirmButton: false
});
}
);
}
</script>
因此,如果您运行此代码,则会在sweetalert的消息框中弹出警告框。
---------- 更新 ---------------
运行这样的代码似乎越来越近了,虽然现在警报消息仍然在结束消息之前发生,但至少这次我可以看到结束消息
function validateSubmit(a,b,c){
var mode = a;
var info = b;
var trunk = c;
var iteration = baseName(info);
if (mode === 'Update' && info != trunk ){
confirmGetMessage(iteration);
}
else {
alert('seems to work')
}
}
function baseName(str) {
var base = new String(str).substring(str.lastIndexOf('/') + 1);
if(base.lastIndexOf(".") != -1)
base = base.substring(0, base.lastIndexOf("."));
return base;
}
function trythis(){
alert('made it right here!');
}
function confirmGetMessage(info){
var message = "<h3>" + info + "</h3><br/>Or Revert Back to Trunk?";
var contmessage = "Updating " + info;
swal({
title: "Update Branch?",
html: message,
type: "question",
showCancelButton: true,
cancelButtonText: "Switch To Trunk",
cancelButtonColor: "#0080FF",
confirmButtonColor: "#DD6B55",
confirmButtonText: "Continue Update",
closeOnConfirm: false,
closeOnCancel: false
}).then(function(){
swal({
text: contmessage,
timer: 1400,
showConfirmButton: false
},trythis())
}, function(dismiss){
if (dismiss === 'cancel'){
swal({
text: 'Switching to Trunk',
timer: 1400,
showConfirmButton: false
})
}
}
)}
答案 0 :(得分:0)
您在创建提醒后的第一个函数调用中将值读入result
,但是您没有检查调用的值。
来自Sweet alert timer - done function,
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) {
if (isConfirm) {
swal({
title: "Deleted!",
text: "Your row has been deleted.",
type: "success",
timer: 3000
});
function () {
location.reload(true);
tr.hide();
};
}
else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
您需要检查输入是否为确认。这样的事情可能就是你想要的,但我没有环境来测试它:
swal({
title: "Update Branch?",
html: message,
type: "question",
showCancelButton: true,
cancelButtonText: "Switch To Trunk",
cancelButtonColor: "#0080FF",
confirmButtonColor: "#DD6B55",
confirmButtonText: "Continue Update",
closeOnConfirm: false,
closeOnCancel: false
},
function(result){
if(result)
{
swal({
text: contmessage,
timer: 1400,
showConfirmButton: false
}), alert('work please');
},
else{
swal({
text: 'Switching to Trunk',
timer: 1400,
showConfirmButton: false
});
}
}
);