Submit The Form When The User Presses Enter

Different browsers have different default behaviors about what to do when the user hits enter in a form. MSIE almost always submits the form, while Netscape will often just beep at you. Although it's usually best to leave the default browser behavior as it is, for some forms people just naturally tend to hit "enter" when they are ready. This is particularly true for login forms. With a little JavaScript we can set the form to submit on enter.

First, copy this script exactly as-is into the <HEAD> section of your document:

<SCRIPT TYPE="text/javascript">

<!--

function submitenter(myfield,e)

{

var keycode;

if (window.event) keycode = window.event.keyCode;

else if (e) keycode = e.which;

else return true;



if (keycode == 13)

   {

   myfield.form.submit();

   return false;

   }

else

   return true;

}

//-->

</SCRIPT>

For each field which should submit the form when they hit enter add an onKeyPress attribute like this:

<FORM ACTION="../cgi-bin/mycgi.pl">

name:     <INPUT NAME=realname SIZE=15><BR>

password: <INPUT NAME=password TYPE=PASSWORD SIZE=10

           onKeyPress="return submitenter(this,event)"><BR>

<INPUT TYPE=SUBMIT VALUE="Log In">

</FORM>

Add the attribute like it is in the example. No modification is needed. This gives us this form:

name:
password:

---------------------------------------------------------------------------------------------------

<script language="javascript">
  var EnterSubmit = function(evt)
  {
           evt = window.event || evt;
      if (evt.keyCode == 13)
      {
               //自己应用代码
               alert('我提交了');
      }
  }
     window.document.onkeydown=EnterSubmit;
 </script>

---------------------------------------------------------------------------------------------------

设表单的name为form1,在其最后一个输入框中添加如下内容:
onKeyDown="if(event.keyCode == 13) javascript:document.form1.submit();"
在firefox 3.0中测试通过.

---------------------------------------------------------------------------------------------------

多浏览器,按回车提交表单的  js 代码如下:
<html>
<head>
<title>多浏览器的回车提交表单</title>
<script>
function login(){
    document.getElementById("myform").submit();
}

function keydown(){
     if(document.addEventListener){//如果是Firefox
         document.addEventListener("keypress",fireFoxHandler, true);
    }else{  //如果是 IE
          document.attachEvent("onkeypress",ieHandler);
     }
}

function fireFoxHandler(evt){
    if(evt.keyCode == 13){   //  Enter 的keycode 是 13
        document.getElementById("btnsubmit").click();//如果是回车键,使用代码触发提交按钮的点击事件。
    }
}

function ieHandler(evt){
 if(evt.keyCode==13){
     if(event.keyCode == 13)// 侦测到按下键盘的按钮,13代表回车键  
      {   
           event.returnValue=false; // 是当前事件的返回值为false  
           event.cancel = true;  // 停止当前时间继续运行  
           document.getElementById("btnsubmit").click();//如果是回车键,使用代码触发提交按钮的点击事件。  
           return false;  
      }
 }
}
</script>
</head>
<body>
<form action="*.do?method=query" method="post" name="myform" id="myform">
<input type="text" name="userName" id="userName" onkeydown="keydown();"/>

<input id="btnsubmit" name="btnsubmit" type="button" onClick="login();" value="提交" class="inputbtn"/>
</form>
</body>
</html>
----------------------------------------------------------------------------------------------以下是转载的文章:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  4. <title>无标题文档</title>  
  5. <script language="javascript">  
  6. function document.onkeydown()  
  7. {  
  8.      if(event.keyCode==13)// 侦测到按下键盘的按钮,13代表回车键  
  9.      {  
  10.           event.returnValue=false// 是当前事件的返回值为false  
  11.           event.cancel = true;  // 停止当前时间继续运行  
  12.           document.getElementById("btnQuery").click();//如果是回车键,使用代码触发提交按钮的点击事件。  
  13.           return false;  
  14.      }  
  15. }  
  16. </script>  
  17.   
  18. <!-- 以下是第二种写法,效果相同 -->  
  19. <script language="javascript">  
  20.     function aa()  
  21.     {  
  22.         if (event.keyCode == 13)  
  23.         {  
  24.             event.returnValue=false;  
  25.             event.cancel = true;  
  26.             document.getElementById("btnQuery").click();  
  27.         }  
  28.     }  
  29. </script>  
  30.   
  31. </head>  
  32.   
  33. <body>  
  34. <form action="b.html" method="post" name="form1" id="form1"  >  
  35.     请输入查询内容:  
  36.     <input name="txtbtsname" type="text" id="txtbtsname" size="60" style="width: 100px; height: 22px"  />  
  37.     <input name="btnQuery" type="submit" id="btnQuery" value="查询" >  
  38. </form>  
  39. </body>  
  40. </html> 

【受用了。以下是自己写的】

//Enter提交账号密码

<SCRIPT TYPE="text/javascript">
<!--
        function document.onkeydown() {
            if (event.keyCode == 13) {
                event.returnValue = false;
                event.cancel = true;
                document.getElementById("btnLogin").click();
                return false;
            }
        }

//-->
</SCRIPT>

<form id="form1" name="form1" action="Login.htm" method="post"></form>

你可能感兴趣的:(submit)