jquery Press the Enter event on the keyboard _jquery

Source: Internet
Author: User

Next we use jquery to implement return enter to switch focus, which is passed in the common browsers IE7, IE8, Firefox 3, Chrome 2, and Safari 4 test.
The development tool used is the Microsoft Vs2010+jquery framework

The following steps are implemented

1, first reference jquery class library

<script src= "Scripts/jquery-1.4.1.min.js" type= "Text/javascript" ></script>

2. JavaScript code

Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ (' Input:text:first '). focus ();
var $inp = $ (' Input:text ');
$inp. Bind (' KeyDown ', function (e) {
var key = E.which;
if (key = = 13) {
E.preventdefault ();
var nxtidx = $inp. Index (this) + 1;
$ (": Input:text:eq (" + Nxtidx + ")"). focus ();
}
});
});
</script>

Analysis:
$ (' Input:text:first '). focus ();

When the page is initialized, focus is positioned in the first text box

var $inp = $ (' Input:text ');

Collection of elements from the type= text box

$inp. Bind (' KeyDown ', function (e) {}

Binding the ' KeyDown ' event to a collection of text boxes

var key = E.which;

The key value of the currently pressed key such as Enter =13

E.preventdefault ();

There are other things that can stop the default behavior of it, where we organize postback, instead of switching focus. Another similar approach is stoppropagation, which plays a role in blocking JS event bubbling.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.