用jQuery屏蔽掉按回车键时提交表单

maoye 2011-04-02

默认情况下,在表单输入框里按回车将会提交表单,但有时需要屏蔽掉按回车键提交表单功能,以保证完整输入表单,如何做呢?

解决办法:

<script type="text/javascript">
	$(function() {
		$("input").keypress(function (e) {
			var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
			if (keyCode == 13) {
				for (var i = 0; i < this.form.elements.length; i++) {
					if (this == this.form.elements[i]) break;
				}
				i = (i + 1) % this.form.elements.length;
				this.form.elements[i].focus();
				return false;
			} else {
				return true;
			}
		);
	});
</script>
 

相关推荐