Enter return switch input focus implementation ideas and code compatible with the major browser _jquery

Source: Internet
Author: User

When doing a project, the customer requests to be able to use enter return directly to switch input (focus), when the last time, direct submission of information.

The first idea is to go online to copy a piece of code directly. But Baidu, Google looked over, found more than 80% of the code is the same. Some code is too old to be used. Some of them are available only in some browsers. There was no proper way to toss for half an hour. The last thought, simply do-it-yourself.

First, train of thought

Each time you click Enter, get the current focus position, and then set its next element to get the focus;

Second, the Code

<script type= "Text/javascript" >
$ (' Input:text:first '). focus (); 
Document.onkeydown = function Enterhandler (event)
{
var inputs = $ ("input");//Add additional filter conditions to 
var browser = Navigator.appname; Browser name
var useragent = navigator.useragent;//Get browser's useragent string 

var Code = ';
if (Browser.indexof (' Internet ') >-1)//IE 
Code = Window.event.keyCode;
else if (Useragent.indexof ("Firefox") >-1)//Firefox
Code = Event.which;
else//other
Code = Event.keycode event.keyCode:event.which? event.which:event.charCode;

if (Code = = 13)//Can add other filter conditions
{for
(var i=0;i<inputs.length;i++)
{
if (inputs[i].id = = Document.activeElement.id)
{ 
i = i== (inputs.length-1)? -1:i;
$ (' # ' + inputs[i+1].id). Focus () break
;
}}} </script>

Among them, because IE and Firefox are different to the key value acquisition, so the browser made a simple distinction between the judgments. This allows you to get the keystroke values on each browser.

Finally, when you get to the current value, you can add a variety of other conditions.

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.