如何防止表格提交?

How to prevent form from submission?

提问人:Jonas 提问时间:11/7/2023 最后编辑:Jonas 更新时间:11/7/2023 访问量:52

问:

我有两个模式:一个是在用户接受时确认付款,它会打开另一个模式以允许上传收据以发送给客户。现在考虑一种情况,我希望通过阻止表单提交来允许用户在选择收据文件后取消上传。

下面是我的模板和 JavaScript 代码,但表单仍在提交中。

let ApprovePayment = document.getElementById('uploadReceiptButton')
if (ApprovePayment) {

  CancelConfirmation = document.getElementById('CancelSubmission')
  if (CancelConfirmation) {

    CancelConfirmation.addEventListener('click', function(event) {
      // Prevent the form submission            
      event.preventDefault();

    });

  }
  ApprovePayment.addEventListener('click', ApprovePaymentFunction);
}

function ApprovePaymentFunction(event) {

  event.preventDefault(); // prevent the link from opening immediately
  var receiptUploadForm = document.getElementById('receiptUploadForm')


  Swal.fire({
    title: 'Are you sure?',
    text: 'Confirm that the correct Receipt is sent to the Applicant',
    icon: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes!'
  }).then((result) => {

    // Remove the Cancel listener, so the form submits when "Yes" is clicked
    CancelConfirmation.removeEventListener('click');
    if (result.isConfirmed) {

      receiptUploadForm.submit();
    }
  });

}
<div class="row">

  <div class="modal fade" id="confirmationModal" aria-hidden="true" aria-labelledby="confirmationModalLabel" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="confirmationModalLabel">Are you sure?</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          Confirm the required amount is received for this service
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button class="btn btn-primary" data-bs-target="#fileUploadModal" data-bs-toggle="modal" data-bs-dismiss="modal">Yes</button>
        </div>
      </div>
    </div>
  </div>

  <div class="modal fade" id="fileUploadModal" aria-hidden="true" aria-labelledby="fileUploadModalLabel" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <form method="POST" action="{% url 'approve-centre-renewal-payment' query.centre_renewal.id %}" id="receiptUploadForm" enctype="multipart/form-data">

          <div class="modal-header">
            <h5 class="modal-title" id="fileUploadModalLabel">Receipt</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>

          <div class="modal-body">

            {% csrf_token %}
            <div class="form-group">
              <label for="receipt">Upload receipt</label>
              <input type="file" id="receipt" name="receipt" class="form-control" accept="application/pdf" required>
            </div>

          </div>
          <div class="modal-footer">
            <button class="btn btn-secondary" data-bs-dismiss="modal" id="CancelSubmission">Cancel</button>
            <button class="btn btn-primary" id="uploadReceiptButton">Upload</button>
          </div>

        </form>

      </div>
    </div>
  </div>

</div>

JavaScript 表单 验证 bootstrap-modal

评论

3赞 CBroe 11/7/2023
CancelConfirmation = document.getElementById('CancelConfirmation')- 在您显示的 HTML 中看不到具有该 ID 的元素。
0赞 mplungjan 11/7/2023
请访问帮助中心参观以查看内容和如何提问。做一些研究。如果您遇到困难,请编辑并发布您的尝试的最小可重现示例,并使用 [<>] 片段编辑器记录输入和预期输出。
0赞 Jonas 11/7/2023
@CBroe我已经更新了被引用的脚本中的 id,谢谢,
0赞 CBroe 11/7/2023
这已经设置好了,所以我猜 Bootstrap 自己的事件处理可能会干扰你在这里的尝试。您应该简单地添加到此按钮,以便它不再是提交按钮 - 然后单击它不会首先提交表单,因此您也不必在之后尝试阻止它。data-bs-dismiss="modal"type="button"
0赞 Jonas 11/7/2023
谢谢大家,我已经设法找出了问题并解决了它,我把一个id放在了错误的行上

答: 暂无答案