Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript表单验证按钮:表单有效后继续下一页_Javascript_Html - Fatal编程技术网

Javascript表单验证按钮:表单有效后继续下一页

Javascript表单验证按钮:表单有效后继续下一页,javascript,html,Javascript,Html,我的表单验证工作得很好,但表单有效后,它不会进入下一页 当我点击提交按钮时 以下是代码的html: <div class="header"> <h2>Create Account</h2> </div> <form id="form" class="form" action="Iphone.html" method=&quo

我的表单验证工作得很好,但表单有效后,它不会进入下一页 当我点击提交按钮时 以下是代码的html:

    <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')

这是由for
e.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
  }
});