打破默认行为绝对不好。 Btw, 您知道 HTML 中的 < code> 自动聚焦 code> 属性吗?
如果你绝对需要这个, 给你:
document.forms.obrazac.onkeypress = function( e ) {
// If the hit key is "Enter"
if ( e.keyCode === 13 ) {
// Cross-browser handling for our dear friend @MaxArt :p
var evt = e || window.event,
target = evt.target || evt.srcElement,
// Find the next input
nextInput = target.nextSibling;
while ( nextInput.tagName !== INPUT && nextInput.nextSibling ) {
nextInput = nextInput.nextSibling;
}
// And focus it
nextInput.focus();
// Finally, disable submitting IF there is no input after
if ( nextInput !== this.elements[ this.elements.length - 1 ] ) {
return false;
}
}
};