javascript - 如何将光标移动到下一个文本字段?

标签 javascript jquery

在我的工作中,我尝试通过按 Enter 键将焦点从一个文本字段移动到另一个文本字段。我在以下链接中找到了可能的答案:

How to go to next textbox when enter is pressed?
Move Cursor to next text Field pressing Enter

但这些都不适合我。我的代码:

<script>
$('input[type='text']').keyup(function(e) {
    if(e.keyCode == 13) {
        $(this).next().focus();
    }
});
</script>
<input type='text' />
<input type='text' />
<input type='text' />
<input type='text' />
<input type='text' />
<input type='text' />

最佳答案

$(document).ready(function () {

	$('form').on('submit', function (event) {
	    event.preventDefault();
	})

	$('input[type="text"]').keyup(function(event) {
	    if(event.keyCode === 13) {
        	$(this).next().focus();
	    }
	});

})
<script src="https://code.jquery.com/jquery-3.2.1.slim.js"></script>
<form>
    	<input type="text"/>
    	<input type="text"/>
    	<input type="text"/>
    	<input type="text"/>
    	<input type="text"/>
</form>

关于javascript - 如何将光标移动到下一个文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192252/

相关文章:

javascript - HTML-如何将当前时间插入网络表格(<td>)?

javascript - 使用 CSS 在单击元素时创建 iPhone 样式滑出

jquery - 使用视口(viewport)单位的响应式设计中的 CSS 文本溢出

javascript - jQuery 元素未传递给 perl cgi

javascript - 在加载完整页面之前使用 JavaScript 获取浏览器记住的滚动位置

javascript - Foundation 6 交换数据源

javascript - Node.js 更改所需对象的值

javascript - IFrame 回发时发生事件?

javascript - 使用 jQuery 从 JSON 树读取特定值

javascript - 为什么不使用搜索方法来查找 .在 JavaScript 中按照我期望的方式工作?