表單中有<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" onclick="showRegist()" tabindex="4" >註冊</button>
</form>
<button type="button" class="flatbtn-blu" onclick="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;
}