jquery 如何从给定数字生成数字,然后在while循环中填充

e5nszbig  于 2022-11-29  发布在  jQuery
关注(0)|答案(1)|浏览(124)

我尝试从from_number值生成数字,然后在循环中填充
假设from_number的值为10 '

<label>
    <input type="text" class="form-control"
           id="from_number" placeholder="from_number"
           autocomplete="off">
</label>

start php while.. I have 50 inputs with the same name ['list']

<label>
    <input type="text" class="form-control" name="list"
           placeholder="list" autocomplete="off">
</label>

结束php
`

$('#from_number').blur(function () {
    let begin = document.getElementById('from_number').value;
    let arr = [];
    let inputs = document.querySelectorAll('[name^="list"]');
    for (let i = 0; i < inputs.length; i++) {
        inputs[i].value = arr.push(begin++) + begin -1;
    }
});

谢谢 预期输出

<input type="text" class="form-control" name="list" value="10" autocomplete="off">
<input type="text" class="form-control" name="list" value="11" autocomplete="off">
<input type="text" class="form-control" name="list" value="12" autocomplete="off">
<input type="text" class="form-control" name="list" value="13" autocomplete="off">
<input type="text" class="form-control" name="list" value="14" autocomplete="off">
<input type="text" class="form-control" name="list" value="50" autocomplete="off">

`

rt4zxlrg

rt4zxlrg1#

document.querySelectorAll()方法返回一个NodeList对象容器,该容器可以作为数组进行迭代。以下代码片段中的事件侦听器在将输入值转换为整数并迭代输入数组之前,检查输入值是否为数字。

const startValueElem = document.getElementById("from_number");

startValueElem.addEventListener("blur", (event) => {
   if (!isNaN(event.target.value)) {
      let value = parseInt(event.target.value);
      Array.from(document.querySelectorAll('[name^="list"]')).forEach(el => {
         el.value = value++;
      });
   }
});

相关问题