简体   繁体   English

使用keyup存储来自输入的不同值

[英]Using keyup to store different values from inputs

I have the following 2 following input tags: 我有以下2个输入标签:

<input class="forLoopIndex" type="text" name="n" size="1">
<input class="forLoopIndex" id="typicalElement" type="text" name="k" size="1" placeholder="k">

Since both they have a class called forLoopIndex. 由于它们两者都有一个名为forLoopIndex的类。 I'm trying to store the values when people type on them using keyup. 当人们使用keyup键入值时,我正在尝试存储这些值。 Like this: 像这样:

    $('.forLoopIndex').keyup(function(){

    forLoopIndex = $(this).val();
    console.log(forLoopIndex);
});

Unfortunately, When I print the value of ForLoopIndex, it only shows the value of the current input where my cursor is. 不幸的是,当我打印ForLoopIndex的值时,它仅在光标所在的位置显示当前输入的值。 Is there any way to store the values of both my inputs after I have typed? 输入后,有什么方法可以存储两个输入的值吗? do I need to use an array? 我需要使用数组吗? any help is more than welcome. 任何帮助都超过了欢迎。

Thanks! 谢谢!

M 中号

Maybe this can help : 也许这可以帮助:

var forloops = $('.forLoopIndex');
var values = [];

forloops.keyup(function(){
    values = [];
    forloops.each(function () {
        values.push($(this).val());
    }
    console.log(values);
});

Whenever the user writes something, the values are updated. 每当用户写东西时,值都会更新。 You can then save them whenever your want. 然后,您可以随时保存它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM