Javascript表单验证按钮:表单有效后继续下一页
我的表单验证工作得很好,但表单有效后,它不会进入下一页 当我点击提交按钮时 以下是代码的html:Javascript表单验证按钮:表单有效后继续下一页,javascript,html,Javascript,Html,我的表单验证工作得很好,但表单有效后,它不会进入下一页 当我点击提交按钮时 以下是代码的html: <div class="header"> <h2>Create Account</h2> </div> <form id="form" class="form" action="Iphone.html" method=&quo
<div class="header">
<h2>Create Account</h2>
</div>
<form id="form" class="form" action="Iphone.html" method="post">
<div class="form-control">
<label for="username">Username</label>
<input type="text" placeholder="Drake" id="username" />
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error message</small>
</div>
<div class="form-control">
<label for="username">Email</label>
<input type="email" placeholder="applehub@gmail.com" id="email" />
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error message</small>
</div>
<div class="form-control">
<label for="username">Password</label>
<input type="password" placeholder="Password" id="password"/>
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error message</small>
</div>
<div class="form-control">
<label for="username">Password check</label>
<input type="password" placeholder="Password two" id="password2"/>
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error message</small>
</div>
<input type="submit" value="Submit" name="">
<a href="#" id="login"><p>Have an account? Login.</p></a>
</form>
</div>
在检查验证是否正确后,需要手动重定向 要重定向到其他页面,您可以使用:
window.location=”http://www.yoururl.com";代码>
如果应用程序是SPA
,请使用browserhistory.push('/uri')代码>这是由fore.preventDefault()引起的代码>。如果调用此函数,则表单中的初始操作将被“禁用”
我认为在这种情况下,最好使用标志。
因此,解决方案可以如下所示:
let is_form_checked = false;
form.addEventListener('submit', e => {
if(!is_form_checked) {
e.preventDefault();
checkInputs();
is_form_checked = true;
//resubmit
}
});
完成验证后,为什么不使用form.submit()代码>您没有告诉它移到下一页吗?谢谢,但它不起作用,如果我第一次单击“提交”按钮表单验证工作,但如果我第二次单击它,它只会移到下一页,即使我没有在字段中添加任何内容,如果您知道我可以为“提交”按钮编写代码的方法,该按钮将在单击表单时验证表单,并在表单有效时继续下一页,而不会影响我在字段中的验证代码,我将不胜感激。谢谢,但当我刷新页面时,它只会转到新页面,而不会返回,请提供更多建议,我们将不胜感激。
let is_form_checked = false;
form.addEventListener('submit', e => {
if(!is_form_checked) {
e.preventDefault();
checkInputs();
is_form_checked = true;
//resubmit
}
});