简体   繁体   English

仅在提交表单之前填写必填字段的情况下,才能创建模式

[英]How to create a modal only if the required fields are filled out before submitting form

I am trying to make a modal appear thanking the user for submitting the form but only if they completely filled out all the required inputs. 我试图使模式出现,感谢用户提交表单,但前提是他们完全填写了所有必需的输入。 I like the built in required feature in HTML but currently both pop ups happen when the submit button is pressed. 我喜欢HTML内置的必需功能,但是当前两个弹出窗口均在按下“提交”按钮时发生。 I know an if else statement is required but cannot figure out how to show that the required field is satisfied in JS before popping up the modal. 我知道是否需要if else语句,但无法在弹出模式之前弄清楚如何显示JS中的必需字段是否满足。

      <!--   THIS IS THE CONTACT FORM SECTION   -->

<section class="contact-form">
  <h1 class="uptop">CONTACT ME</h1>
  <form method="get">
    <input type="text" class = "name required" id="name" placeholder="NAME" required>
    <input type="email" class="email required" id="email" placeholder="EMAIL" required>
    <textarea id="message" class="message required" rows="5" cols="300" placeholder="MESSAGE"></textarea>
    <input type="submit" class="submit" id="submit" value="Tell Me Something Good" >
  </form>
</section>


<!-- The Modal -->
<div id="myModal" class="modal">
  <div class="modal-content">
  <span class="close">&times;</span>
  <p class ="modal-message">THANKS FOR THAT!</p>
  </div>
</div

JavaScript: JavaScript的:

var modal = document.getElementById('myModal');
var btnSubmit = document.getElementById("submit");
var spanClose = document.getElementsByClassName("close")[0];
var requiredFields = document.getElementsByClassName("required").required;


btnSubmit.onclick = function() {
modal.style.display = "block";

}


spanClose.onclick = function() {
modal.style.display = "none";
}

window.onclick = function(event) {
   if (event.target == modal) {
    modal.style.display = "none";
  }
}

I think this could help The innerHTML will do the job 我认为这可以帮助innerHTML胜任

 function check(){ var name = document.getElementById('name').value; if(name!=''){ setStatus("Success"); }else{ setStatus("Error"); } } function setStatus(message){ var inputStatus = document.getElementById('msg'); inputStatus.innerHTML = message; } 
  <input id='name' name='name' /><span id="msg"></span> <button type="submit" onClick="check()">Send</button> 

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

相关问题 如何创建需要填写的条件字段 - How to create conditional fields that are required to be filled out 如果未填写必填字段,如何防止提交表单上的函数调用? - How to prevent function call on submit form if required fields are not filled out? 如何在提交表单之前要求所有输入字段? - How to make all input fields be required before submitting a form? 提交前如何填写所有输入字段 - how to make all input fields to be filled before submitting 未填写必填字段时停止提交表单 - Stop form submission when the required fields have not been filled out 如何检查表单中所有必填字段的填写时间? - How can I check when all of the required fields of a form has been filled out? 仅在表单字段有效且已填写时才显示模式框-JQuery - Only show modal box if the form fields are valid and filled in - JQuery GTM - 表单提交时的Fire标记(仅当填写了必填字段时) - GTM - Fire tag on form submit (only if required fields are filled) 如何仅在使用 bootstrap 填写所有必需的 attr 字段后才提交表单? - How to submit a form only if all the required attr fields are filled using bootstrap? 提交未填写必填字段的表单时无法停止函数执行 - Can't stop function execution when submitting a form with required fields not filled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM