此代码可将Enter键转换为选项卡。但是我想根据tabIndex进行工作,并且在提交输入之前将其放置在最后一个输入上就完成了提交:
<script>
document.addEventListener('keydown', function (event) {
if (event.keyCode === 13 && event.target.nodeName === 'INPUT' ) {
var form = event.target.form;
var index = Array.prototype.indexOf.call(form, event.target);
form.elements[index + 1].focus();
event.preventDefault();
}
});
</script>
答案 0 :(得分:1)
您可以尝试以下操作
document.addEventListener('keydown', function (event) {
if (event.keyCode === 13 && event.target.nodeName === 'INPUT' ) {
var form = event.target.form;
var index = Array.prototype.indexOf.call(form, event.target);
if(form.elements[index + 1]) {
form.elements[index + 1].focus();
} else {
alert('Ready to submit');
//form.submit();
}
}
});
<form action="#" method="post">
<input placeholder="Enter your first name"/><br/><br/>
<input placeholder="Enter your second name"/><br/><br/>
<input placeholder="Enter your email"/><br/><br/>
<input placeholder="Choose password"/>
</form>
答案 1 :(得分:0)
<script language="JavaScript">
document.addEventListener('keydown', function (event) {
if (event.keyCode === 13 && event.target.nodeName === 'INPUT' && event.target.type!=='submit' ) {
var tidx = +(event.target.tabIndex) +1,elems = document.getElementsByTagName('input');
for (var i=elems.length; i--;) {
var tidx2 = elems[i].getAttribute('tabindex');
var tidx3 = elems[i].getAttribute('type');
if (tidx2 == tidx && tidx3!='submit') {elems[i].focus();
event.preventDefault(); } }
}
}
);
</script>