捕捉用户的输入,用回车提交

一、整个页面用一个回车提交事件:

<input type="button" value="回车提交" id="auto" onclick="alert('你按了回车')"/>
<script type="text/javascript">
    document.onkeydown = function(e){
        if(!e) e = window.event;//火狐中是 window.event
        if((e.keyCode || e.which) == 13){
            document.getElementById("auto").click();
        }
    }

</script>

二、某个输入框中注册回车事件,一般用于用户在页面输入后按回车:

<script>
function enterIn(evt){
  var evt=evt?evt:(window.event?window.event:null);//兼容IE和FF
  if (evt.keyCode==13){
  var obj ;
  queryDistrict(obj,2);
}
}
</script>

<input type="text" id ="input1" maxlength="3" onkeydown="enterIn(event);"/> 

如果一个页面可能需要捕捉多个回车事件,那么就利用上面的方式分开写

原文地址:https://www.cnblogs.com/wills2010/p/1799604.html