简体   繁体   English

如何检测使用JavaScript的输入字段?

[英]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.

相关问题 如何使用 Javascript 更改从输入字段收到的日期格式? - How To Change Date Format Received From Input Field Using Javascript? 如何从通过 Javascript function 动态创建的输入字段访问表单输入? - How can I access form input from an input field which was created dynamically via a Javascript function? 如何仅使用JavaScript在输入字段中拆分数据? - How can I split data in an input field using only JavaScript? 如何使用 JavaScript 在输入字段中输入值? - How do I input the value in input field using JavaScript? 如何使用JavaScript从div的输入字段中获取输入值 - How can I get my input value from input field in a div using JavaScript 如何通过点击JavaScript中输入文本字段内的交叉按钮来刷新实时搜索? - How can I refresh my live search by hitting on the cross button which is inside the input text field in JavaScript? 如何动态删除使用JavaScript动态创建的输入字段 - How to dynamically remove input field which is created dynamically using JavaScript 我如何使用 Jquery 检测输入值何时发生变化 - How i can detect when input values are change using Jquery 如何使用 javascript 创建一个 function,它将检查字段值/用户输入并在输入正确时显示警报? - How to create a function using javascript which will check a field value/user input and show alert if the input is correct? 为什么我无法从javascript中获得输入值 - Why I can not received the value in input from javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM