js 回車提交表單

 一、整個頁面用一個回車提交事件:

  1. <input type="button" value="回車提交" id="auto" onclick="alert('你按了回車')"/> 
  2. <script type="text/javascript"> 
  3.     document.onkeydown = function(e){ 
  4.         if(!e) e = window.event;//火狐中是 window.event 
  5.         if((e.keyCode || e.which) == 13){ 
  6.             document.getElementById("auto").click(); 
  7.         } 
  8.     } 
  9.  
  10. </script> 

二、某個輸入框中註冊回車事件,一般用於用戶在頁面輸入後按回車:

  1. <script> 
  2. function enterIn(evt){ 
  3.   var evtevt=evt?evt:(window.event?window.event:null);//兼容IE和FF 
  4.   if (evt.keyCode==13){ 
  5.   var obj ; 
  6.   queryDistrict(obj,2); 
  7. </script> 
  8.  
  9. <input type="text" id ="input1" maxlength="3" onkeydown="enterIn(event);"/>  

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章