Javascript If语句,然后继续

时间:2018-10-25 10:07:57

标签: javascript jquery sweetalert

我正在运行一个if语句,并检查一下与momentjs不同的日期。如果语句为真,我将如何停止整个函数的执行,然后在其中决定是继续整个函数还是退出?

例如,我有一个名为store的函数

$(document).on('click', '#new-booking-form input[type="submit"]', function(e){
    e.preventDefault();

    //If the difference in months is more than 3
    if(moment($('input[name="my_input"]').val() , 'DD/MM/YYYY').diff(moment(), 'months') > 3){
            swal({
                title: 'Warning',
                html: 'The starting date you have selected is more than 3 months in the future, is this correct?',
                type: 'warning',
                customClass: 'swal-logout',
                showCancelButton: true,
                confirmButtonText: 'Yes'
            }).then((result) => {
                if (result.value) {
                    return true;
                }
            });
    }



   //Do the submit here
})

我需要此功能的工作方式是

  • 如果if语句为false,请运行其余函数。
  • 如果if语句为true,则提示用户,如果用户选择yes,则运行其余功能。
  • 如果if语句为true,则提示用户;如果用户选择否,则返回false,并且不运行该函数。

如何根据if语句以及如果用户选择是或否来运行整个功能?

1 个答案:

答案 0 :(得分:0)

不确定这是否是您要寻找的东西,但这听起来像是您在试图强行解决诺言

function errorHandler(error) {
  throw(error)
};

if(moment($('input[name="my_input"]').val() , 'DD/MM/YYYY').diff(moment(), 'months') > 3){
        swal({
            title: 'Warning',
            html: 'The starting date you have selected is more than 3 months in the future, is this correct?',
            type: 'warning',
            customClass: 'swal-logout',
            showCancelButton: true,
            confirmButtonText: 'Yes'
        }).then((result) => {
            if (result.value) {
                return true;
            }
        }, errorHandler);
}

希望有帮助

相关问题