JS 文本框回車觸發(fā)事件

AngularJS 的用法

首先在html中導(dǎo)入Angularjs

<script type="text/javascript" src="js/angular.min.js"></script>

在html中,放置兩個文本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="admin" ng-controller="modify">
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body>
    <input type="text" id="name" name="name" rows="10" cols="20" ng-keypress="myKeypress($event)" />
    <input type="text" id="address" name="address" rows="10" cols="20" />
</body>
<script type="text/javascript" src="modify.js"></script>
</html>

最后modify.js代碼中處理回車事件:

var app = angular.module('admin', []);
app.controller('order', function($scope, $http) {
    
    $scope.myKeypress = function(e){
        var keycode = window.event?e.keyCode:e.which;
        if(keycode == 13){
            document.getElementById("address").focus();
    }
};
    

JQuery 的用法

在html中,放置幾個文本

<input type="text" id="name1" name="name" onkeypress="EnterPress(event, 0, 1)" onkeydown="EnterPress()" />
<input type="text" id="address1" name="address" onkeypress="EnterPress(event, 1, 1)" onkeydown="EnterPress()" />
 
<input type="text" id="name2" name="name" onkeypress="EnterPress(event, 0, 2)" onkeydown="EnterPress()" />
<input type="text" id="address2" name="address" onkeypress="EnterPress(event, 1, 2)" onkeydown="EnterPress()" />

當(dāng)前文本聚焦時,回車光標(biāo)移動到下一個文本框中
js處理:

function EnterPress(e, type, nameId){ //傳入 event   
    var e = e || window.event;
    if(e.keyCode == 13){
       if (type == 0) {
          document.getElementById("address" + nameId).focus(); 
       } else {   
          var a = parseInt(nameId);
          a += 1;
         document.getElementById("name" + a).focus(); 
       } 
    }
 }   
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容