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.