Javascript 甜蜜警报继续提交表单确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32164151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:42:22  来源:igfitidea点击:

sweet alert continue submitting form on confirm

javascriptjqueryformssweetalert

提问by Omar Makled

I've this sweetalert triggered on submit of a form.

我在提交表单时触发了这个甜蜜。

$(".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() {

        }
        );
    });

but on clicking confirm I want it to continue submiting the form...

但点击确认我希望它继续提交表单......

Bad ideas come to mind, like:

坏主意浮现在脑海中,例如:

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();
        });
    }
});

or moving swa to button click instead of on submit, and using on submit of a form.

或将 swa 移动到按钮单击而不是提交,并在提交表单时使用。

but I don't like this solution, it looks frankesteini to me. Surely there is a better way

但我不喜欢这个解决方案,对我来说它看起来像 frankesteini。肯定有更好的方法

回答by Omar Makled

$('#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();
    });
});

回答by Eddy

I know this is late but it might help someone in the future, I've used this with success for submitting forms with sweetalert confirmations.

我知道这已经晚了,但它可能会在将来对某人有所帮助,我已经成功地使用它来提交带有 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

            }
        });
});

回答by timgavin

Here's another example, asking the user to confirm via checkbox.

这是另一个示例,要求用户通过复选框进行确认。

HTML

HTML

<form id="myForm">
    <button type="submit" id="btn-submit">Submit</button>
</form>

JavaScript

JavaScript

$(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();
    });
});

回答by Francisco Rangel

I believe this is a little more elegant:

我相信这更优雅一点:

$(".swa-confirm").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(){
        $(".swa-confirm").off("submit").submit();
    });
});

回答by pradeep

function testalert(){

功能 testalert(){

swal(

    {
        title              : "Are you sure?",
        text               : "You want to save?",
        type               : "warning",
        allowEscapeKey     : false,
        allowOutsideClick  : false,
        showCancelButton   : true,
        confirmButtonColor : "#DD6B55",
        confirmButtonText  : "Yes",
        showLoaderOnConfirm: true,
        closeOnConfirm     : false
    }, 

    function (isConfirm) {

        if (isConfirm) {
            profileinfo.submit();
            return true;
        }

        return false;

    }

);

}

}

回答by gecko

100% Working!!

100%工作!

Do some little trick using attribute. In your form add an attribute like data-flagin your form, assign "0" as false.

使用属性做一些小技巧。在您的表单中添加一个类似data-flag的属性,将“0”指定为 false。

<form class="swa-confirm" data-flag="0">
    //your inputs
</form>

In your jquery.

在你的jQuery中。

$(".swa-confirm").on("click", function(e) {
    $flag = $(this).attr('data-flag');

    if($flag==0){
        e.preventDefault(); //to prevent submitting

        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 ){
                // if confirmed change the data-flag to 1 (as true), to submit
                $('.swa-confirm').attr('data-flag', '1');
                $('.swa-confirm').submit();
            }
        });
    }

    return true;
});

回答by Samiran

You can use this

你可以用这个

$('#deleteamc').on('click',function(e) {
    
    event.preventDefault();
var form = this;
    
        swal({
  title: "Are you sure?",
  text: "All data related to this AMC ID will be parmanently deleted",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, DELETE it!",
  cancelButtonText: "No, cancel please!",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  if (isConfirm) {
   form.submit();
   } else {
    swal("Cancelled", "AMC Record is safe :)", "error");
 
  }
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<form method="POST" id="deleteform">
        <input type="hidden" name="amccode" id="amccode" value="<?php echo $row['amccode']; ?>">
        <button class="btn btn-danger" id="deleteamc" name="delete" type="submit">
            <i class="fa fa-minus"></i>
                Delete
        </button>
</form>

回答by Aries

var form = $(this).parents('form');   
    swal({
        title: "Are you sure?",
        icon: "warning",
        buttons:[
            'No, cancel it!',
            'Yes, I am sure!'
            ],
            }).then(function(isConfirm){        
                if(isConfirm){ 
                        form.submit();
                } 
});

回答by Rafael Cuevas

Apply the class .swa-confirm to the input submit

将类 .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();
    });
});

First prevent the button event. After, launch the sweetalert and if "confirmed" is true then submit #my-form element.

首先防止按钮事件。之后,启动sweetalert,如果“confirmed”为真,则提交#my-form 元素。

Sorry for my bad english, regards from Mexico, I used Google translate :v.

抱歉我的英语不好,来自墨西哥的问候,我使用了谷歌翻译:v。