javascript - Ajax 在这两种情况下都运行

标签 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 !");
        });

最佳答案

使用 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!");
        });
    });
});

关于javascript - Ajax 在这两种情况下都运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34044616/

相关文章:

c# - 表单提交取消 jQuery ajax 调用

jQuery通过POST发送HTML数据

javascript - 背景模糊 - CSS

php - Chrome 兼容性错误

javascript - 哪个是学习React JS的最佳地方?

javascript - 没有 JavaScript 的 Ajax

javascript - 当我单击表格行时,如何将焦点设置在最近的输入字段上。类似于没有文本的 <label>

javascript - 如何将值从 javascript 发送到服务器端 (asp.net)?

javascript - 使用jquery设置输入元素的值

javascript - 在打印预览对话框中,背景颜色或背景 CSS 设置未呈现