[英]How can I detect which input field received input using JavaScript?
I have the following HTML code: 我有以下HTML代码:
<div>
<input id="input1" type="text" size="50" autofocus autocomplete="on">
</div>
<div>
<input id="input2" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input3" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input4" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input5" type="text" size="50" autocomplete="on">
</div>
<div>
<input id="input6" type="text" size="50" autocomplete="on">
</div>
Now, when a user types something in any input field and then press enter, I need to recognize which one received the input, then store that input's ID in a var and the typed content into another. 现在,当用户在任何输入字段中键入内容,然后按Enter键时,我需要识别出哪个收到了输入,然后将该输入的ID存储在var中,并将键入的内容存储到另一个中。 So far I have this code which I use to recognize if enter has been pressed:
到目前为止,我拥有以下代码,可以用来识别是否已按下Enter键:
window.onload = function() {
searchinput = document.getElementById("input1");
if(!!searchinput) {
searchinput.addEventListener("keypress", function(a) {
var key = a.keyCode;
if(key == 13) {
var query = this.value;
search(query);
}
});
}
};
But as you can see, it only works with a specific input field (input1 in the example), and it doesn't insert anywhere the element's ID. 但是正如您所看到的,它仅适用于特定的输入字段(示例中为input1),并且不会在元素ID的任何位置插入。
You can use onkeypress
event to detect user key press and e.wich
to check if the pressed key is code equal 13
(enter key), then get attribute you want from current input using this
, check example below. 您可以使用
onkeypress
事件检测用户按下的按键,并使用e.wich
检查所按下的按键是否等于13
(输入键),然后使用this
从当前输入中获取所需的属性,请e.wich
下面的示例。
Hope this helps. 希望这可以帮助。
document.onkeypress = function(e){ if(e.which==13){ //click on enter key alert(e.target.id+'---'+e.target.value); // get the id and value of current input } }
<div> <input id="input1" type="text" size="50" autofocus autocomplete="on"> </div> <div> <input id="input2" type="text" size="50" autocomplete="on"> </div> <div> <input id="input3" type="text" size="50" autocomplete="on"> </div> <div> <input id="input4" type="text" size="50" autocomplete="on"> </div> <div> <input id="input5" type="text" size="50" autocomplete="on"> </div> <div> <input id="input6" type="text" size="50" autocomplete="on"> </div>
I would use jQuery
. 我会使用
jQuery
。
This is untested code, but something like this might work. 这是未经测试的代码,但类似的方法可能有效。
// 1. Give all input fields a class of all_inputs.
// 2. The following code should loop through each one and add the ID to the all_ids array.
var all_ids = [];
$('.all_inputs').each(function() {
var elem = $(this);
all_ids.push($(this).attr('id'));
});
$(document).on("keypress", "input", function (event) { var intKeyCode = parseInt(event.keyCode ? event.keyCode : event.which); if (intKeyCode === 13) { alert("Input Id : " + $(this).attr("id") + " Input Value : " + $(this).val()); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input id="input1" type="text" size="50" autofocus autocomplete="on"> </div> <div> <input id="input2" type="text" size="50" autocomplete="on"> </div> <div> <input id="input3" type="text" size="50" autocomplete="on"> </div> <div> <input id="input4" type="text" size="50" autocomplete="on"> </div> <div> <input id="input5" type="text" size="50" autocomplete="on"> </div> <div> <input id="input6" type="text" size="50" autocomplete="on"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.