js文本框焦点自动聚焦到下个文本框
HTML:
<form> <input type="text" name="text1" maxlength="3" size="3"/>- <input type="text" name="text2" maxlength="3" size="3"/>- <input type="text" name="text3" maxlength="3" size="3"/> </form>
js:
function tabForward() { if(this.value.length == this.getAttribute('maxlength')) { for(let i = 0;i < this.form.elements.length;i ++) { if(this == this.form.elements[i]) { if(this.form.elements[i + 1]) { this.form.elements[i + 1].focus(); return; } } } } } var form = document.forms[0]; var text1 = form.elements['text1']; var text2 = form.elements['text2']; var text3 = form.elements['text3']; text1.addEventListener('keyup', tabForward, false); text2.addEventListener('keyup', tabForward, false); text3.addEventListener('keyup', tabForward, false);