Ajax正在两种情况下运行

时间:2015-12-02 13:59:02

标签: javascript jquery ajax sweetalert

当我确认提交值并获得成功警报时,我已经创建了一个输入框,但我也在取消按钮上取得了成功?

swal({
    title: " Asking price for " + askData[0] + "?",
    text: "If you are sure press OK or edit this text",
    type: "input",
    inputType: "text",
    inputValue: "Hello " + askData[1] + ", I am interested in buying your " + askData[0] + " of Variety :" + askData[3] + ". Please update the Price!",
    showCancelButton: true,
    closeOnConfirm: false,
    showLoaderOnConfirm: true
}, function (inputValue) {
    if (inputValue === "") {
        swal.showInputError("Please write something !");
        return false;
    }
    $.ajax({
        url: urlEnq,
        type: "GET"
    });
    $.ajax({
        url: 'saveUserMessage',
        type: "POST",
        data: {
            fieldUserId: fieldUserId,
            filedUserCompanyId: filedUserCompanyId,
            message: inputValue
        },

    })
        .done(function (data) {
            swal("Done!", "Your message has been successfully sent.", "success");
        })
        .error(function (data) {
            swal.showInputError("Please write something !");
        });

1 个答案:

答案 0 :(得分:2)

使用SweetAlert时,如果点击取消,则传入回调函数的变量值将为false而不是''

我已更新您的jsfiddle以显示此信息,请参阅以下更新代码:

$('button.delete-account').click(function (e) {
    swal({
        title: "Are you sure you want to delete your account?",
        text: "If you are sure, type in your password:",
        type: "input",
        inputType: "password",
        showCancelButton: true,
        closeOnConfirm: false
    }, function (typedPassword) {
        if (typedPassword === false) {
            return;
        }
        if (typedPassword === "") {
            swal.showInputError("You need to type in your password in order to do this!");
            return false;
        }
        $.ajax({
            url: "/api/delete-account",
            data: { password: typedPassword },
            type: "POST"
        }).done(function (data) {
            swal("Deleted!", "Your account has been deleted!", "success");
        }).error(function (data) {
            swal.showInputError("Your password is wrong!");
        });
    });
});