1. 程式人生 > >jQuery回車鍵快速切換下一個input文字框解決方案

jQuery回車鍵快速切換下一個input文字框解決方案

表單中有很多文字框,為了輸入方便使用回車鍵切換到下一個文字框。下面是實現步驟。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>enter</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
</head>
<body>
    <div onkeyup
="onEnter()">
<input type="text" name="input" onclick="clickEnter()"> <input type="text" name="input" onclick="clickEnter()"> <input type="text" name="input" onclick="clickEnter()"> <input type="text" name="input" onclick="clickEnter()"> <input
type="text" name="input" onclick="clickEnter()">
<input type="text" name="input" onclick="clickEnter()"> <input type="text" name="input" onclick="clickEnter()"> </div> <script> var index = 0 function clickEnter() { $('input[name=input]'
).each(function (i) { if ($(this).is(':focus')) { index = i } }) } function onEnter() { if (event.keyCode == 13){ index++ if (index >= $('input[name=input]').length) { index = 0 } $('input[name=input]').each(function (i) { if (i == index) { $(this).focus() } }) } }
</script> </body> </html>