当我确认它提交值并获得成功警报时,我已经创建了一个输入框,但我在取消按钮上也获得了成功?
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/