gpt4 book ai didi

javascript - 响应甜蜜警报 2 后触发功能

转载 作者:行者123 更新时间:2023-12-03 05:43:47 28 4
gpt4 key购买 nike

我已经被这个问题困扰了几天了。我正在使用 sweetalert2,我想要完成的是根据给出的答案触发一个 javascript 函数,所以如果我单击“确定”,它会触发一件事,如果我单击“取消”,它会执行其他操作。我可以让它工作,但它似乎在甜蜜警报完成之前触发了该功能。这是代码示例:

<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
})
}
}
)}

最佳答案

您在创建警报后的第一个函数调用中将值读入结果,但您没有检查调用的值。
来自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
});
}
}
);

关于javascript - 响应甜蜜警报 2 后触发功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40410993/

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