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

标签 javascript jquery sweetalert

我已经被这个问题困扰了几天了。我正在使用 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/

相关文章:

javascript - ajax调用后如何初始化Datatable

javascript - jQuery:如何检查某些复选框是否被选中,并相应地更改样式?

ajax - CakePHP AJAX 调用

javascript - 如何在 sweetalert 中设置字符限制?

javascript - SweetAlert isConfirm 无法正常工作

javascript - 根据提供的 JQuery Snippet 修改 HTML

javascript - 在函数中使用 jQuery .append()?

Javascript 在浏览器中获取和设置可用性

javascript - 尽管使用 .on(),动态创建的元素不会触发事件

javascript - 延迟打开链接,同时显示警报 jquery