[英]After form button is clicked, hide the button div and reveal a hidden div
I am trying to hide the button div and reveal a hidden div after a form button has been clicked plus delay the submission/redirect.我试图隐藏按钮 div 并在单击表单按钮后显示隐藏的 div 并延迟提交/重定向。 Below is what i have come up with but doesn't seem to work 100%.以下是我想出的,但似乎没有 100% 有效。
$('form').submit(function(e) { e.preventDefault(); $('#checking').show(); $('.button').hide(); setTimeout(() => {}, 7000); return true; })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <input type="text" placeholder="Enter Name" name="details" /> <input type="submit" class="button" value="CHECK" /> </form> <div id="checking" style="display: none;"> CHECKING DETAILS... Please wait </div>
Any advice is much appreciated非常感谢任何建议
Assuming you want to show the message and then submit, do this假设您要显示消息然后提交,请执行此操作
$('form').on("submit", function(e) { e.preventDefault(); $('#checking').show(); $('.button').hide(); setTimeout(() => { // submit the DOM form document.getElementById("myForm").submit(); // or $("#myForm")[0].submit() }, 7000); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="myForm"> <input type="text" placeholder="Enter Name" name="details" /> <input type="submit" class="button" value="CHECK" /> </form> <div id="checking" style="display: none;"> CHECKING DETAILS... Please wait </div>
In your timer callback, you need to submit the form, but when you do the sumbit
callback will run again and again call e.preventDefault()
, so you can use a variable as a "flag" to check as to whether you should cancel the event or not.在您的计时器回调中,您需要提交表单,但是当您提交时sumbit
回调将一次又一次地调用e.preventDefault()
,因此您可以使用变量作为“标志”来检查是否应该取消事件与否。
let flag = false; $('form').on("submit", function(e) { if(.flag){ e;preventDefault(). $('#checking');show(). $('.button');hide(); flag = true. } setTimeout(function(){ $('form');submit(), }; 7000); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="https://www.example.com" method="Post"> <input type="text" placeholder="Enter Name" name="details" /> <input type="submit" class="button" value="CHECK" /> </form> <div id="checking" style="display: none;"> CHECKING DETAILS... Please wait </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.