【问题标题】:Form Submit Confirmation With SweetAlert2使用 SweetAlert2 提交确认表单
【发布时间】:2019-03-20 05:44:17
【问题描述】:

我正在处理一个需要在提交表单之前进行确认的项目,为此我使用的是 SweetAlert2,但它无论如何都不起作用。我尝试使用来自另一个线程的代码,但我遇到了同样的问题,页面加载,当我提交表单时,它什么也没做(嗯,它提交的信息没有验证或问题)。它已经像没有 SweetAlert 的测试版一样工作了。

HTML 表单是这样的:

´<form class="form-register col-8" method="post" action="insert.php" enctype="multipart/form-data">
<select name="subjects"> 
  <option disabled="disabled">Select</option>
  <--!Here I have PHP code to list things from DB -->
</select>
<input id="m" type="text"/>
<select name="tempo">
  <option disabled="disabled">Select<option> <!--Regular options list-->
<select>
<input id="c" type="text"/>
<input type="submit" id="btn-ok" value="OK" name="register" class="btn btn-primary accept"/>
</form>´

我没有 SweetAlert 的功能脚本:

$(document).ready(function(){
$(".form-register").submit(function(){
 if ($("#m").val().length<9){
  alertify.warning('Incomplete ID');
  return false;
 }
 if ($("#m").val().length>9){
  alertify.warning('Error');
  return false;
 }
 if ($("#c").val().length>200){
  alertify.warning('Write less comments please');
  return false;
 }
 else
 {
    var Con= confirm("Click ok if you are sure");
    if(Con == true)
    {
      return true;
    }
    {
      return false;
    }
 }
});

});

注意:我使用的是来自 jsdelivr 的 SweetAlert2 8.X。 我试图在一个函数中分离这个脚本并将其包含在 else 中(即使我退出了前两个 $ 函数,也能以 50% 的速度工作):

$(document).ready(function(){
$('.form-register').submit(function (e){
  e.preventDefault();
  const swalWithBootstrapButtons = Swal.mixin({
    customClass: {
      confirmButton: 'btn btn-success',
      cancelButton: 'btn btn-danger'
    },
    buttonsStyling: false,
  })
  swalWithBootstrapButtons.fire({
    title: 'Are you  sure?',
    text: "Check plz",
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: 'OK',
    cancelButtonText: 'Cancel',
    reverseButtons: true
  }).then((result) => {
    if (result.value) {
      swalWithBootstrapButtons.fire(
        'Finished',
        'Success',
        'success',
      ), function(submit){
        form.submit();
        }
    } else if (
      result.dismiss === Swal.DismissReason.cancel
    ) {
      swalWithBootstrapButtons.fire(
        'Canceled',
        'Do corrections and then retry :)',
        'error'
      )
    }
  })
});

});

我尝试用一​​个按钮替换输入中的提交类型,并在脚本头部替换另一个选择器。我唯一需要的是确认继续在 insert.php 上插入数据,如果用户取消返回另一条错误消息,如果可能的话......提前致谢。

【问题讨论】:

    标签: javascript php jquery forms sweetalert2


    【解决方案1】:

    变化:

    <input type="submit" id="btn-ok" value="OK" name="register" class="btn btn-primary accept"/>
    

    收件人:

    <input type="button" id="btn-ok" value="OK" name="register" class="btn btn-primary accept"/>
    

    然后,在您的 JS 中,监听“#btn-ok”的点击,然后使用 $form.submit() 以编程方式提交表单。见下文:

    $(document).ready(function() {
        $('form #btn-ok').click(function(e) {
            let $form = $(this).closest('form');
    
            const swalWithBootstrapButtons = Swal.mixin({
                customClass: {
                    confirmButton: 'btn btn-success',
                    cancelButton: 'btn btn-danger'
                },
                buttonsStyling: false,
            })
    
            swalWithBootstrapButtons.fire({
                title: 'Are you  sure?',
                text: "Check plz",
                type: 'warning',
                showCancelButton: true,
                confirmButtonText: 'OK',
                cancelButtonText: 'Cancel',
                reverseButtons: true
            }).then((result) => {
                if (result.value) {
                    swalWithBootstrapButtons.fire(
                            'Finished',
                            'Success',
                            'success',
                        );                     
                    $form.submit();
                } else if (
                    result.dismiss === Swal.DismissReason.cancel
                ) {
                    swalWithBootstrapButtons.fire(
                        'Canceled',
                        'Do corrections and then retry :)',
                        'error'
                    );
                }
            });
    
        });
    

    【讨论】:

    • 非常感谢...它几乎可以 100% 工作,但现在我不知道为什么表单没有提交到页面 insert.php,我只收到 SWA2 的消息和然后它不会重定向或加载 PHP 代码。有什么建议吗?谢谢
    • 只有 TypeError: g 未定义,但它是引导程序的错误。与表单的动作无关
    • 我修改了我的答案。可以试试吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2017-01-20
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多