12 javascript forms jquery sweetalert
我在提交表格时触发了这个甜蜜的事情.
$(".swa-confirm").on("submit", function(e) {
e.preventDefault();
swal({
title: $(this).data("swa-title"),
text: $(this).data("swa-text"),
type: "warning",
showCancelButton: true,
confirmButtonColor: "#cc3f44",
confirmButtonText: $(this).data("swa-btn-txt"),
closeOnConfirm: false,
html: false
}, function() {
}
);
});
Run Code Online (Sandbox Code Playgroud)
但点击确认我希望它继续提交表格...
想到了不好的想法,比如:
var confirmed = false;
$(".swa-confirm").on("submit", function(e) {
var $this = $(this);
if (!confirmed) {
e.preventDefault();
swal({
title: $(this).data("swa-title"),
text: $(this).data("swa-text"),
type: "warning",
showCancelButton: true,
confirmButtonColor: "#cc3f44",
confirmButtonText: $(this).data("swa-btn-txt"),
closeOnConfirm: true,
html: false
}, function() {
confirmed = true;
$this.submit();
});
}
});
Run Code Online (Sandbox Code Playgroud)
或者将swa移动到按钮点击而不是提交,并使用提交表单.
但我不喜欢这个解决方案,它看起来像frankesteini给我.当然有更好的方法
Oma*_*led 29
$('#btn-submit').on('click',function(e){
e.preventDefault();
var form = $(this).parents('form');
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!",
closeOnConfirm: false
}, function(isConfirm){
if (isConfirm) form.submit();
});
});
Run Code Online (Sandbox Code Playgroud)
小智 6
我知道这已经晚了,但它可能会在将来对某人有所帮助,我已经成功地使用它来提交带有 Sweetalert 确认的表单。
$('#form_id').submit(function (e, params) {
var localParams = params || {};
if (!localParams.send) {
e.preventDefault();
}
//additional input validations can be done hear
swal({
title: "Confirm Entry",
text: "Are you sure all entries are correct",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#6A9944",
confirmButtonText: "Confirm",
cancelButtonText: "Cancel",
closeOnConfirm: true
}, function (isConfirm) {
if (isConfirm) {
$(e.currentTarget).trigger(e.type, { 'send': true });
} else {
//additional run on cancel functions can be done hear
}
});
});
Run Code Online (Sandbox Code Playgroud)
这是另一个示例,要求用户通过复选框进行确认。
<form id="myForm">
<button type="submit" id="btn-submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
$(document).on('click', '#btn-submit', function(e) {
e.preventDefault();
swal({
title: 'Confirm',
input: 'checkbox',
inputValue: 0,
inputPlaceholder: ' I agree with the Terms',
confirmButtonText: 'Continue',
inputValidator: function (result) {
return new Promise(function (resolve, reject) {
if (result) {
resolve();
} else {
reject('You need to agree with the Terms');
}
})
}
}).then(function (result) {
$('#myForm').submit();
});
});
Run Code Online (Sandbox Code Playgroud)
将类 .swa-confirm 应用到输入提交
$(".swa-confirm").on("click", function(e) {
e.preventDefault();
swal({
title: $(this).data("swa-title"),
text: $(this).data("swa-text"),
type: "warning",
showCancelButton: true,
confirmButtonColor: "#cc3f44",
confirmButtonText: $(this).data("swa-btn-txt"),
closeOnConfirm: true,
html: false
}, function( confirmed ) {
if( confirmed )
$(this).closest('form').submit();
});
});
Run Code Online (Sandbox Code Playgroud)
首先阻止按钮事件。之后,启动 sweetalert,如果“已确认”为 true,则提交 #my-form 元素。
抱歉我的英语不好,来自墨西哥的问候,我使用了谷歌翻译:v。