按Enter键向下一个文本框移动

本文介绍了如何通过按下Enter键在网页表单中流畅地从一个文本框移动到下一个,提高用户交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	
	
</head>
<body>
	<form action="" method="get">
		<table>
			<tbody>
				<tr>
					<td><input type="text" onblur="" enter="yes" name=""></td>
					<td><input type="text" onblur="" enter="yes" name=""></td>
					<td><input type="text" onblur="" name="" readonly></td>
					<td><input type="text" onblur="" enter="yes" name=""></td>
					<td><input type="text" onblur="" enter="yes" name=""></td>
					<td><input type="text" onblur="" enter="yes" name=""></td>
					<td><input type="text" onblur="" enter="yes" name=""></td>
				</tr>
			</tbody>
		</table>
	</form>
	
	<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
	<script>
		$('body').on('keydown', 'textarea', function(e) {
			var self = $(this);
			var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
			if (eCode == 13) {
				e.preventDefault();
			}

		});

		/**
		 * 回车键focus定位
		 */

		$('body')
			.on('keyup', 'input, select, textarea',
				function(e) {
					console.log(e);
					var self = $(this),
						form = self.parents('form:eq(0)'),
						focusable, next, prev;
					var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
					// shift+enter 光标向上个元素移动
					if (e.shiftKey) {
						if (e.keyCode == 13) {
							// 排除只读,disabled元素
							focusable = form.find('input,a,select,textarea')
								.filter(':visible').not(':input[readonly]')
								.not(':input[disabled]');
							// focusable =
							// form.find('input,a,select,textarea').filter(':visible');
							prev = focusable.eq(focusable.index(this) - 1);

							if (prev.length) {
								if ($(this).attr("shiftEnter") == "no") {
									return false;
								} else {
									prev.focus();
								}
							}
							// else {
							// form.submit();
							// }
						}
					} else
						// Ctrl+enter 在textaera中换行
						if (e.ctrlKey && eCode == 13 &&
							this.localName == "textarea") {

							var myValue = "\n";
							var $t = $(this)[0];
							if (document.selection) { // ie<9
								this.focus();
								var sel = document.selection.createRange();
								sel.text = myValue;
								this.focus();
								sel.moveStart('character', -l);
								var wee = sel.text.length;
							}
							// 现代浏览器
							else if ($t.selectionStart || $t.selectionStart == '0') {
								var startPos = $t.selectionStart;
								var endPos = $t.selectionEnd;
								var scrollTop = $t.scrollTop;
								$t.value = $t.value.substring(0, startPos) +
									myValue +
									$t.value.substring(endPos,
										$t.value.length);
								this.focus();
								// 因为myValue回车显示为\n
								$t.selectionStart = startPos + myValue.length;
								$t.selectionEnd = startPos + myValue.length;
								$t.scrollTop = scrollTop;

							} else {
								this.value += myValue;
								this.focus();
							}

						} 
						else
							// enter 光标向下个元素移动
							if (eCode == 13) {
								if (this.localName == "textarea") {
									e.preventDefault();
									e.stopPropagation();
								}
								// this.context.css("background-color","#b3d7f4");
								// 排除只读,disabled元素
								focusable = form.find('input,select,textarea').filter(
									':visible').not(':input[readonly]').not(
									':input[disabled]');
								// focusable =
								// form.find('input,select,textarea').filter(':visible');

								next = focusable.eq(focusable.index(this) + 1);

								// 下个元素存在
								if (next.length) {
									// console.log(this.id +" "+ next[0].id);
									// var nid = next[0].id;
									// $("#" + nid).css("background-color", "#b3d7f4");
									// $("#" + this.id).css("background-color", "");
									if ($(this).attr("enter") == "no") {
										return false;
									} 
									else {
										next.focus();
									}


								}

								return false;
							}

		});
	</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值