阻止點選回車提交表單效果程式碼例項

antzone發表於2017-04-01

本章節介紹一下如何阻止點選回車提交表單效果。

在預設情況下,如果表單或者表單元素獲取焦點的話,點選回車就會提交表單。

但是在實際應用中,往往需要只點選提交按鈕才能夠提交表單。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.softwhy.com/" /> 
<title>螞蟻部落</title> 
<script>
window.onload=function(){
  var oform=document.getElementById("theform");
  oform.onkeydown=function(ev){
    var ev=ev||window.event;
    var keycode = ev.which || ev.keyCode;
    return keycode == 13 ?false : true;
  }
}
</script> 
</head> 
<body> 
<form name="theform" id="theform" action="http://www.softwhy.com">
  <div><input type="text"></div>
  <div><input type="text" ></div>
  <div><input type="text" ></div>
  <div><input type="text" ></div>
  <div><input type="text" ></div>
  <div><input type="submit" value="檢視效果"></div>
</form>
</body> 
</html>

上面的程式碼實現了我們的要求,程式碼比較簡單這裡就不多介紹了。

相關文章