简体   繁体   English

如何在成功提交表单时创建弹出模式?

[英]How do I create a pop up modal on successful form submission?

I have an HTML form connected to formsubmit.co form endpoint.我有一个连接到formsubmit.co表单端点的 HTML 表单。

 <form action="https://formsubmit.co/my@email.com" method="POST" class="form">
     <input type="text" name="name" required>
     <input type="email" name="email" required>
     <button type="submit">Submit</button>
</form>

I've added <input type="hidden" name="_next" value="https://mydomain.co/thanks.html"> to redirect back to the original page after submission is required.我添加了<input type="hidden" name="_next" value="https://mydomain.co/thanks.html">以在需要提交后重定向回原始页面。 I've also disabled the reCAPTCHA using <input type="hidden" name="_captcha" value="false"> , so when the form is submitted, the page just reloads.我还使用<input type="hidden" name="_captcha" value="false">禁用了 reCAPTCHA,因此当提交表单时,页面会重新加载。

The problem is the user doesn't know anything has happened after form submission, the page just loads for some time and then reloads.问题是用户在提交表单后不知道发生了什么,页面只是加载了一段时间然后重新加载。 I don't want to create another page for this, I actually want to use a modal for it.我不想为此创建另一个页面,我实际上想为此使用模式。

I tried adding a submit event listener, and when that event is triggered, an alert is shown;我尝试添加一个submit事件监听器,当该事件被触发时,会显示一个alert but the alert shows even before the form submission process begins.但警报甚至在表单提交过程开始之前就会显示。

function alertSubmit() {
  alert('Your details were successfully received.');
}

const form = document.querySelector('form');
form.addEventListener('submit', alertSubmit);

How can I display an alert immediately after the form is successfully submitted and not when the submit button is clicked?如何在表单成功提交后而不是在单击submit按钮时立即显示alert

You can do following changes:您可以进行以下更改:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <form action="https://formsubmit.co/your@email.com" method="POST" class="form"> <input type="text" name="name" required> <input type="email" name="email" required> <button type="submit">Submit</button> </form> <script> $(document).ready(function(){ $('.form').on('submit', function(){ alert('Your details were successfully received.'); }); }); </script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<form action="https://formsubmit.co/your@email.com" method="POST" class="form">
     <input type="text" name="name" required>
     <input type="email" name="email" required>
     <button type="submit">Submit</button>
</form>
<script>
    $(document).ready(function(){
        $('.form').on('submit', function(){
            swal("Title", "Message Content", "success", {
  button: "Ok",
});
        });
    });
</script>

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

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