Next, we use Jquery to implement the Enter key to switch the focus. This code is tested in popular browsers such as IE7, IE8, Firefox 3, Chrome 2, and Safari 4.
The development tool used is the Microsoft VS2010 + Jquery framework.
The implementation steps are as follows:
1. First reference the Jquery class library
<Script src = "Scripts/jquery-1.4.1.min.js" type = "text/javascript"> </script>
2. Javascript code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ('Input: text: first '). focus ();
Var $ indium = $ ('input: text ');
$ Indium. bind ('keylow', function (e ){
Var key = e. which;
If (key = 13 ){
E. preventDefault ();
Var nxtIdx = $ indium. index (this) + 1;
$ (": Input: text: eq (" + nxtIdx + ")"). focus ();
}
});
});
</Script>
Analysis:
$ ('Input: text: first '). focus ();
When the page is initialized, focus on the first text box
Var $ indium = $ ('input: text ');
Type = set of elements in the text box
$ Indium. bind ('keylow', function (e ){}
Bind the 'keylow' event to the text box collection
Var key = e. which;
Take the currently pressed key value, for example, Enter key value = 13
E. preventDefault ();
It can prevent other things from happening due to its default behavior. Here we organize PostBack, but switch the focus. Another similar method is stopPropagation, which can prevent js event bubbling.