通过<input>标签

updating values of an array through <input> tags

本文关键字:标签 input 通过      更新时间:2023-09-26

我有以下三个标签:

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

现在我有了一个事件侦听器,它检查何时有值传入,然后将其存储在数组中。我希望数组只保留3个值。因为我要用第三个和第二个来做点什么,但我要看看它们什么时候变了。下面是它的JS:

  forloops.keyup(function () {
    if (forLoopIndex.length < 2 && forLoopIndex >= 0) {
        forloops.each(function () {
            forLoopIndex.push($(this).val());
            appendingToSigmaLimit();
            console.log(sigmaLimit.val());
            console.log(forLoopIndex);
        });
    } else if (forLoopIndex.length > 2) {
        forLoopIndex = [];
    }
});

现在,问题是,这些值只会更新,直到我再次更改了三个输入的值。我有一种感觉,我的JS中的逻辑方式正在使它这样做。我只需要在每次改变其中一个输入的值时更新值。什么好主意吗?

谢谢,

不确定您期望的是什么,这样的代码将分别更新每个输入

var forloops= $('.forLoopIndex');
var forLoopIndex = [];
forloops.keyup(function () {
    forloops.each(function (i, e) {
           forLoopIndex[i] = $(this).val();
           console.log(i);
           console.log(forLoopIndex);
    });
});

小提琴

编辑不带循环:

var forloops= $('.forLoopIndex');
var forLoopIndex = [];
forloops.keyup(function () {
    forLoopIndex[$(this).index('.forLoopIndex')] = $(this).val();
    console.log(forLoopIndex);
});

小提琴