【问题标题】:Unable to submit form in javascript after sweet alert has been closed关闭甜蜜警报后无法在 javascript 中提交表单
【发布时间】:2021-04-20 19:45:47
【问题描述】:

我创建了一个具有演示登录功能的应用程序。演示登录将弹出一个甜蜜的警报,让用户知道他们所做的更改不会保存到数据库中。问题是在用户按下将关闭甜蜜警报的确定按钮后表单没有提交。按下确定按钮后提交表单的正确语法是什么?

下面是我的html

  <form method="post" id="submitForm" name="demoSubmitForm">
    <div class="demo-button pt-3">
      <button type="button" name="demoEmail" value="DemoEmail" class="au-btn au-btn--block au-btn--blue 
        m-b-20" onclick="ShowSweetAlert()">
        Demo Application
      </button>
    </div>
  </form>

下面是我的脚本:

function ShowSweetAlert() {
    Swal.fire({
        icon: 'warning',
        title: 'You are logging in as a Demo User!',
        text: 'Changes you make to the Application will not be saved once you log out.',
        onClose: document.getElementById('submitForm').submit()     /*document.demoSubmitForm.submit()*/
    });
}

目前document.demoSubmitForm.submit()document.getElementById('submitForm').submit() 都无法提交表单。

Video of the Problem

【问题讨论】:

    标签: javascript html sweetalert2


    【解决方案1】:

    添加这个Script:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多