【问题标题】:Sweet alert confirmation box does not seem to be working with form甜蜜警报确认框似乎不适用于表单
【发布时间】:2021-10-22 23:08:56
【问题描述】:

我正在使用SweetAlert2 向用户显示一个确认框,如果他选中了这样的表单上的复选框:

<form id="from1" data-flag="0" action="" method="POST">
    @csrf
    <input type="checkbox" name="wallet_checked" onchange="this.form.submit();">
</form>

如您所见,我已将onchange="this.form.submit();" 用于复选框,以便在不使用提交按钮的情况下提交表单,它工作正常。

然后为了显示 Sweet Alert 确认消息,我添加了以下内容:

      document.querySelector('#from1').onsubmit = function(e){
            $flag = $(this).attr('data-flag');
            if($flag==0){
                e.preventDefault(); //to prevent submitting
                swal({
                        title: "Are you sure?",
                        text: "You are about to create a transaction",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: '#DD6B55',
                        confirmButtonText: 'Yes, I am sure!',
                        cancelButtonText: "No, cancel it!",
                        closeOnConfirm: false,
                        closeOnCancel: false
                    },
                    function(isConfirm){
                        if (isConfirm){
                            swal("Shortlisted!", "Transaction successfully created", "success");
                            //update the data-flag to 1 (as true), to submit
                            $('#from1').attr('data-flag', '1');
                            $('#from1').submit();
                        } else {
                            swal("Cancelled", "Transaction didn't submitted", "error");
                        }
                    });
            }
            return true;
        };

但问题是,当我点击复选框时它不会弹出。

在控制台上,没有出现错误。

那么当用户选中复选框时如何正确显示此确认消息?

【问题讨论】:

  • 使用onclick="this.form.submit();"然后检查提交
  • @A.ANoman 我试过了,还是没有显示确认信息
  • 你可以关注这个链接stackoverflow.com/questions/833032/…
  • $flag = $(this).attr('data-flag'); 它包含什么值0 or 1?控制台您的$flag 数据

标签: javascript jquery laravel checkbox sweetalert


【解决方案1】:

首先您说您正在使用 sweetalert2,但您为 alert 编写的代码是为 sweetalert 而不是为 sweetalert2。两者的语法不同。

试试这个代码,这应该可以工作:

<form id="from1" data-flag="0" action="" method="POST">
@csrf
    <input type="checkbox" id="checkbox" name="wallet_checked">
    <label>checkbox</label>
</form>

<script>

    $(function(){
        $('#checkbox').on('change',function(){
            let flag = $('#from1').attr('data-flag');
            if(flag == 0){

                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: '#DD6B55',
                    confirmButtonText: 'Yes, I am sure!',
                    cancelButtonText: "No, cancel it!",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }).then((result) => {
                    if (result.isConfirmed) {
                        Swal.fire("Shortlisted!", "Transaction successfully created", "success");
                        // update the data-flag to 1 (as true), to submit
                        $('#from1').attr('data-flag', '1');
                        $('#from1').submit();
                    } else {
                        Swal.fire("Cancelled", "Transaction didn't submitted", "error");
                    }
                })
            }
        });
    });
        
</script>

【讨论】:

  • 您遇到什么错误?你用的是什么sweetalert cdn?
  • 没有出现错误。它只是执行操作而不显示任何 SweetAlert 弹出消息。
  • 好的,首先尝试 console.log(flag),尝试检查你是否获得了 flag 值。然后尝试使用简单的 alert("test");现在
  • console.log(flag) 没有任何价值!
  • 我尝试了另一个代码,它显示了 SweetAlert 弹出消息,但现在 isConfirm 出现了一些问题,请检查一下:stackoverflow.com/questions/68878812/…
猜你喜欢
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2015-09-17
  • 2017-08-30
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
相关资源
最近更新 更多