提交前确认甜蜜警报2



我正在尝试在提交表单之前弹出确认。但这行不通。我用的是Sweet Alert 2.

document.querySelector('#order').addEventListener('submit', function(e) {
        var form = $(this).parents('form');
        e.preventDefault();
        swal({
            title: "Are you sure?",
            text: "Once a invoice is created, you will not be able to delete without the help of support",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
        }).then(function() {
            swal({
                title: 'Success!', 
                text: 'Invoice created! Go to the invoice tab to pay it.', 
                type: 'success'
            }, function() {
                form.submit();
            });
        },function(dismiss) {
            if(dismiss == 'cancel') {
                swal("Cancelled", "Invoice not created!", "error");
            }
        });
    });

这是我的Javascript代码PHP代码是这样的

 <?php
if(isset($_POST['k'])
{
header('Location: https://google.com');
}
?>
我的HTML代码是
<form method="POST">
<button type="submit" name="k"></button
</form>

为什么不工作?


更新
$("#order").on('submit', function(e) {
        var form = $(this);
        e.preventDefault();
        swal({
            title: "Are you sure?",
            text: "Once a invoice is created, you will not be able to delete without the help of support",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
        }).then(function() {
            swal({
                title: 'Success!', 
                text: 'Invoice created! Go to the invoice tab to pay it.', 
                type: 'success'
            }, function() {
                $(this).trigger('submit');
            });
        },function(dismiss) {
            if(dismiss == 'cancel') {
                swal("Cancelled", "Invoice not created!", "error");
            }
        });
    });

我忘了添加我解析过的代码在我的实际代码中表单

上的ID

不工作的意思是数据没有被发布。如果它工作正常,它应该重定向到google.com根据PHP代码

我不是编码员,但我发现将提交置于.then(function(){下是我的工作解决方案。我使用的代码是jquery ("div. jquery ")。主要形式形式")。submit ();但是您的表单名称会有所不同。我希望这对你有帮助。

$("#order").on('submit', function(e) {
    var form = $(this);
    e.preventDefault();
    swal({
        title: "Are you sure?",
        text: "Once a invoice is created, you will not be able to delete without the help of support",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: '#DD6B55',
        confirmButtonText: 'Yes, I am sure!',
        cancelButtonText: "No, cancel it!",
    }).then(function() {
      $(this).trigger('submit');
        swal({
            title: 'Success!', 
            text: 'Invoice created! Go to the invoice tab to pay it.', 
            type: 'success'
        }, function() {
            $(this).trigger('submit');
        });
    },function(dismiss) {
        if(dismiss == 'cancel') {
            swal("Cancelled", "Invoice not created!", "error");
        }
    });
});

首先,在这个HTML中没有选择器#order

其次,您想要阻止表单元素的默认行为,所以这样做:
$("form").on('submit', function(e) {
    var form = $(this);
    e.preventDefault();
    //[...]

然后,在你想最终提交的地方,你可以这样写:

$(this).trigger('submit');

最新更新