使用 SweetAlert2 表单提交确认

Form Submit Confirmation With SweetAlert2

我正在做一个项目,在提交表单之前需要确认,为此我正在使用 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 上插入数据,如果用户取消 return 如果可能的话,另一条错误消息...提前致谢。

变化:

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

    });