简体   繁体   English

单击 sweetalert 的确认按钮后,表单没有提交

[英]form didn't submit after I click the confirm button of sweetalert

What's wrong with my code?我的代码有什么问题? sweetalert is working and it will pop up if i click the button in my form but when I click the confirm button in sweetalert the form did't submit in other words nothing happened, sweetalert 正在运行,如果我单击表单中的按钮,它会弹出,但是当我单击 sweetalert 中的确认按钮时,表单没有提交,换句话说,什么也没发生,

can someone help me with this?有人可以帮我弄这个吗?

<form class="" action="gotospoilage.php?id=<?php echo $id; ?>" method="post">
  <input
    type="text"
    maxlength="2"
    name="spoilnum"
    class="spoil"
    style="font-weight: bold; color: #000"
    required
  />
  <button class="btn btn-warning spoilbtn"><i class="icon-trash"></i></button>
</form>
<script>
  $(".spoilbtn").on("click", function (e) {
    e.preventDefault;
    Swal.fire({
      title: "Spoiling...",
      text: "Are you sure you want to Spoil this Item?",
      icon: "warning",
      showCancelButton: true,
      confirmButtonColor: "red",
      cancelButtonColor: "gray",
      confirmButtonText: "Reset",
    }).then((result) => {
      if (result.isConfirmed) {
        form.submit();
      }
    });
  });
</script>

Couple of issues...几个问题...

  1. preventDefault is a function and needs () added to call it preventDefault是一个 function 需要添加()来调用它
  2. form is not defined form未定义

You really don't need jQuery for this.你真的不需要 jQuery 这个。 I would attach a submit event on the <form> so you can more easily reference the event target in order to submit it我会在<form>上附加一个提交事件,这样您就可以更轻松地引用事件目标来提交它

document
  .querySelector("form[action^='gotospoilage.php']")
  .addEventListener("submit", async (e) => {
    e.preventDefault(); // 1️⃣
    const { isConfirmed } = await Swal.fire({
      // ...
    });
    if (isConfirmed) {
      e.target.submit(); // 2️⃣
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM