HTML禁止表單提交方法

有時在點擊提交按鈕時不希望提交form表單,所以在寫提交按鈕時應該注意以下幾點:

<form name="form1" method="post" action="" >
	<table width="50%" border="0" cellspacing="1" cellpadding="0">
		<tr>
			<td width="20%">用戶名:</td>
			<td width="50%">
				<label>
				 <input type="text" required="required" placeholder="請輸入用戶名" id="username" v-model="username" />
				</label>
			</td>
			<td width="30%"><div id="username_error"></div></td>
		</tr>

		<tr>
			<td colspan="3">
				<label>
				 <input type="button" @click="register" name="button" id="button" value="提交"> 
				 <input type="reset" name="button2" id="button2" value="重置">
				</label>
			</td>
		</tr>
	</table>
</form>

一、在寫<input> 提交按鈕時type類型不要再寫成submit改爲button類型,然後爲按鈕添加一個點擊事件就可以了,如上述的按鈕寫法,不要在form中寫onsubmit屬性。

 <input type="button" @click="register" name="button" id="button" value="提交"> 

二、注意不要再表單中寫這種類型的<button>提交</button>按鈕默認也是提交整個表單。
三、可以再form中添加onsubmit屬性,讓提交的返回值爲false,這樣就能控制點擊提交按鈕提交表單了
如:

<form  method="post" action=""  onsubmit="return false;">

或在form中寫一個提交的方法,讓方法做一些驗證,不符合條件阻止提交,如:

<form  method="post" action=""  onsubmit="return check();">
--------------------------------------------------------------
// 驗證表單
function check(){
	// 驗證表單數據是否符合條件,不符合返回false禁止提交
	return false;			
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章