HTML防止input回車提交表單

原文鏈接:https://blog.csdn.net/ligang2585116/article/details/44699567

版權聲明:本文爲CSDN博主「奮飛」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ligang2585116/article/details/44699567

自動提交情況說明:

1.默認情況下,單個輸入框,無論按鈕的type="submit"還是type="button"類型,回車即提交。
2.當type="submit"時,無論有幾個type="text"輸入框,回車均表示提交。(按鈕默認的type爲submit)
3.當type="button"時,且存在多個輸入框,回車不提交。(button)

解決方案:

1.解決單個輸入框的回車即提交問題,可以增加一個隱藏的input=“text” display=‘none’; 然後type類型爲button。
2.在form表單或input中加入:οnkeydοwn=“if(event.keyCode==13){return false;}”



實例一:
<!-- enter會自動提交數據 -->
<form action="www.baidu.com" method="post">
	<input type="text" value="" />
	<input type="text" value="" />
	<button>提交</button>
	<!--等價於<button type="submit">提交</button>或<input type='submit' value='提交'>-->
</form>

實例二:
<!-- enter不會自動提交數據 -->
<form action="www.baidu.com" method="post">
	<input type="text" value="" />
	<input type="text" value="" />
	<button type="button">提交</button>
	<!-- 等價於<input type='button' value='提交'> -->
</form>

實例三:
<!-- enter不會自動提交數據 -->
<form action="www.baidu.com" method="post" οnkeydοwn="if(event.keyCode==13){return false;}">
	<input type="text" value="" />
	<input type="text" value="" />
	<button>提交</button>
	<!-- 或在對應input上添加,同時建議取消自動填充,因爲mac下會有問題<input type="text" value="" autocomplete="off" οnkeydοwn="if(event.keyCode==13){return false;}" /> -->
</form>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章