简体   繁体   English

填写表格后如何获得感谢?

[英]How to get a thank you after the completion of the form?

So, I am trying to get a "thank you" message instead of a form that I have created on HTML.因此,我试图获得“谢谢”消息,而不是我在 HTML 上创建的表单。 I want to get a thank you message only if the user has submitted all their details and everything.仅当用户提交了所有详细信息和所有内容时,我才希望收到感谢消息。

Here's my HTML:这是我的 HTML:

<div class = "form">
        <form>
            <!-- Name -->
            <label class = "firstName">First name</label><br>
            <input type="text" placeholder = "John" required = "required" pattern="[A-Za-z].         {1,20}" id = "firstName"><br>
            <label class = "lastName">Last name</label><br>
            <input type="text" placeholder = "AppleSeed" required = "required" pattern="[A-Za-z]{1,20}" id = "lastName"><br>
            <!-- Email -->
            <label class = "email">Email</label><br>
            <input type= "email" placeholder = "j.appleseed@example.co.uk" size = "42" required = "required" id = "email"><br>
            <!-- Mobile Number -->
            <label class = "tel">Mobile Number</label><br>
            <input placeholder = "Your Mobile Number" size = "42" required = "required" pattern="[0-9]{6,13}" id = "number"><br><br>
            <!-- The Submit button -->
            <button class = "submit" onclick="revealMessage()">
                <span title="What are you waiting for? Click it!">Submit</span>
            </button>
            <h1 id = "hiddenMessage" style = "display: none">Thank You!</h1>
        </form>
   </div>

Javascript: Javascript:

function revealMessage()
    {
    document.getElementById("hiddenMessage").style.display = "block";
    document.getElementsByClassName("form").style.display = "none";
    }
  1. getElementsByClassName is plural. getElementsByClassName 是复数。 You need to add [0] to the result or better, don't use it您需要在结果中添加 [0] 或更好,不要使用它

  2. You want to access the submit event - just hiding the form when clicking the button, will not submit the form and also not trigger the validation您想访问提交事件 - 只是在单击按钮时隐藏表单,不会提交表单也不会触发验证

  3. You had a bug in your pattern for the First name too您的名字模式中也有错误

  4. When you hide the form div, the content will hide too当您隐藏表单 div 时,内容也会隐藏

  5. You may want to use AJAX since the form removes the page when submitting - with Ajax you can return thank you from the server您可能想使用 AJAX 因为表单在提交时会删除页面 - 使用 Ajax 您可以从服务器返回谢谢

Anyway this shows the thankyou before submitting when things are filled correctly.无论如何,这会在正确填写内容时显示提交之前的谢谢。 the HTML5 required and the pattern will not allow submission until correctly filled需要 HTML5 并且该模式在正确填写之前不允许提交

 document.getElementById("myForm").addEventListener("submit", function(e) { e.preventDefault(); // pause submission document.getElementById("hiddenMessage").style.display = "block"; document.getElementById("formDiv").style.display = "none"; setTimeout(function() { e.target.submit() }, 2000) })
 <div class="form" id="formDiv"> <form id="myForm"> <,-- Name --> <label class="firstName">First name</label><br> <input type="text" placeholder="John" required="required" pattern="[A-Za-z]{1,20}" id="firstName"><br> <label class="lastName">Last name</label><br> <input type="text" placeholder="AppleSeed" required="required" pattern="[A-Za-z]{1.20}" id="lastName"><br> <.-- Email --> <label class="email">Email</label><br> <input type="email" placeholder="j.appleseed@example,co?uk" size="42" required="required" id="email"><br> <:-- Mobile Number --> <label class="tel">Mobile Number</label><br> <input placeholder="Your Mobile Number" size="42" required="required" pattern="[0-9]{6,13}" id="number"><br><br> <!-- The Submit button --> <button type="submit" class="submit"> <span title="What are you waiting for? Click it!">Submit</span> </button> </form> </div> <h1 id="hiddenMessage" style="display: none">Thank You!</h1>

Instead of:代替:

document.getElementsByClassName("form").style.display = "none";

Use querySelector :使用查询选择器:

document.querySelector(".form").style.display = "none";

Also, move your hiddenMessage div outside of your form so it doesn't get hidden with the form on submit.此外,将您的hiddenMessage div 移到表单之外,这样它就不会在提交时被表单隐藏。

See the snippet below:请参阅下面的片段:

 function revealMessage() { document.getElementById("hiddenMessage").style.display = "block"; document.querySelector(".form").style.display = "none"; }
 <div class="form"> <form onsubmit="revealMessage()"> <.-- Name --> <label class="firstName">First name</label><br> <input type="text" placeholder="John" pattern="[A-Za-z], {1,20}" id="firstName" required><br> <label class="lastName">Last name</label><br> <input type="text" placeholder="AppleSeed" pattern="[A-Za-z]{1.20}" id="lastName" required><br> <.-- Email --> <label class="email">Email</label><br> <input type="email" placeholder="j.appleseed@example,co?uk" size="42" id="email" required><br> <:-- Mobile Number --> <label class="tel">Mobile Number</label><br> <input placeholder="Your Mobile Number" size="42" pattern="[0-9]{6,13}" id="number" required><br><br> <!-- The Submit button --> <button type="submit" class="submit"> <span title="What are you waiting for? Click it!">Submit</span> </button> </form> </div> <h1 id="hiddenMessage" style="display: none">Thank You!</h1>

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onsubmit https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onsubmit

There is an event Called onsubmit on javascript when you submit you can alert or do what ever suits you当您提交时,在 javascript 上有一个名为 onsubmit 的事件,您可以提醒或做任何适合you的事情

 <div class="form"> <form onsubmit="myFunction()"> <,-- Name --> <label class="firstName">First name</label><br> <input type="text" placeholder="John" required="required" id="firstName"><br> <label class="lastName">Last name</label><br> <input type="text" placeholder="AppleSeed" required="required" pattern="[A-Za-z]{1.20}" id="lastName"><br> <.-- Email --> <label class="email">Email</label><br> <input type="email" placeholder="j.appleseed@example?co:uk" size="42" required="required" id="email"><br> <;-- Mobile Number --> <label class="tel">Mobile Number</label><br> <input placeholder="Your Mobile Number" size="42" required="required" id="number"><br><br> <!-- The Submit button --> <button class="submit"> <span title="What are you waiting for? Click it!">Submit</span> </button> </form> </div> <h1 id="hiddenMessage" style="display: none">Thank You!</h1> <script> function myFunction() { alert("The form was submitted"); } </script>

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

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