如何屏蔽按回车提交form表单的动作

来源:互联网 发布:盘古数据 2017业绩 编辑:程序博客网 时间:2024/06/05 19:15

有时候你希望form表单的提交 通过自己的事件提交,而不需要按回车键就自动提交,甚至,有时对你来说按回车键提交了还会产生错误或你不想要的结果,这是该怎么处理呢?可以通过截获键盘事件,判断到是回车的时候返回false.

  var   Nav4   =   document.layers;  
  var   IE4   =   document.all;  
   
  function enterkey(e)  
  {  
  //alert("In   EnterKey   function");  
   
   if   (Nav4)  
   {  
    keyPressed   =   String.fromCharCode(e.which);  
   }   
   else if (IE4)  
   {  
   // alert("In   IE4");  
           keyPressed   =   String.fromCharCode(window.event.keyCode);  
   }  
    
   if(keyPressed   ==   "/r"   ||   keyPressed   ==   "/n")  
         {  
         //   alert("In   keypress");  
     return   (false);  
   }    
  }
 
    if   (window.document.captureEvents   !=   null)  
  window.document.captureEvents(Event.KEYPRESS)  
  window.document.onkeypress   =   enterkey;