表单中有<button>一旦点击就会自动提交表单,要给<button>加上type=“button”属性才可以
阻止表单提交
<form action="" method="GET" onsubmit = "return login_check()">
<label for="username">用户名:</label><br/>
<input placeholder="用户名" type="text" name="username" value="" id="username" tabindex="1"/>
<br/><label for="password">密码:</label><br/>
<input placeholder="密码" type="password" name="password" value="" id="password" tabindex="2" />
<button type="submit" class="flatbtn-blu" tabindex="3" >登录</button>
<button type="button" class="flatbtn-blu" οnclick="showRegist()" tabindex="4" >注册</button>
</form>
<button type="button" class="flatbtn-blu" οnclick="showRegist()" tabindex="4" >注册</button>
如果没有type=“button” 注册按钮也会提交表单
login_check()返回值为true时才会提交表单
function login_check(){
if(document.getElementById("username").value == ""){
alert("用户名不能为空!");
return false;
}
if(document.getElementById("password").value == ""){
alert("密码不能为空!");
return false;
}
return true;
}