gpt4 book ai didi

jquery - 如何找到按下 Enter 时哪个输入字段获得焦点? (jquery)

转载 作者:行者123 更新时间:2023-12-01 03:37:46 24 4
gpt4 key购买 nike

我的表单中有 3 个输入字段(文本字段)。

<input id="input1" name="input1" type="text" value="">
<input id="input2" name="input2" type="text" value="">
<input id="input3" name="input3" type="text" value="">

我也有这段javascript:

$("#timetable").on("submit", function(event) {
console.log("input " + $("input").attr("id") + "has focus");
});

我想知道按下回车/返回按钮时哪个输入字段具有焦点。我在网上搜索过,但一无所获。

谢谢

最佳答案

MDN - Document.activeElement

Document.activeElement returns the currently focused element, that is, the element that will get keystroke events if the user types any. This attribute is read only.

因此,在提交事件监听器中,您将访问 document.activeElement ,这是焦点元素。

Example Here

$("#timetable").on("submit", function (event) {
console.log(document.activeElement); // element
console.log(document.activeElement.id); // element id

$(document.activeElement).css('border-color', 'red');
});

关于jquery - 如何找到按下 Enter 时哪个输入字段获得焦点? (jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com