When working on a project, the customer requires that you can use enter Enter to directly switch the input (focus) and submit the information directly when the last one.
The first idea is to copy a piece of code online and use it directly. But after searching Baidu and Google, more than 80% of the codes found are the same. Some codes are too old to use. Some of them are available to some browsers. After half an hour of hard work, there was no suitable solution. At the last thought, just do it yourself.
1. Ideas
Each time you click Enter, get the current focus position, and then set its next element to get the focus;
2. Code
<script type="text/javascript">$('input:text:first').focus(); document.onkeydown = function enterHandler(event){var inputs = $("input"); //You can add other filter conditions by yourself var browser = navigator.appName; //Browser name var userAgent = navigator.userAgent; //Get the 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 Codes = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;if (Code == 13) //You can add other filter conditions yourself{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 have different key-value acquisitions, a simple judgment and distinction is made on the browser. This allows you to get the key values of the tap on each browser.
Finally, after obtaining the current value, you can add various other conditions.