一、整個頁面用一個回車提交事件:
- <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 evtevt=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);"/>