dsiftnc99059 2017-03-11 11:37
浏览 701

如何使用javascript将值插入到输入数组中

I am generating row of input with arrays.

@foreach($msp_temp as $current)
 <td>
 <input type="number" step ="0.01" class="required" name="firstEntry[]" id="firstEntry">
 </td>
 <td>
 <input type="number" step ="0.01" class="required" name="secondEntry[]" id="secondEntry">
 </td>
 ...

I have a variable set somewhere like:

var entrysum = 0

I want to insert the variable value (entrysum) inside the [] and increase the variable every loop.

How do I insert entrysum value into firstEntry[], secondEntry[], and increase the variable value by 1 after every loop?

  • 写回答

4条回答 默认 最新

  • dounang1974 2017-03-11 11:45
    关注

    I will assume that as per code, there are always a couple of firstEntry and secondEntry and I understand that you want to update them together with the same value.


    First, you get all the input numbers whose name starts with firstEntry and another for secondEntry:

    var firstEntries = document.querySelectorAll('[name^="firstEntry"]');
    var secondEntries = document.querySelectorAll('[name^="secondEntry"]'); 
    

    Second, you iterate all elements and autoincrement the value.

    for (var i = 0; i < firstEntries.length; i++) {
      firstEntries[i].value = entrysum; // update firstEntry
      secondEntries[i].value = entrysum; // update secondEntry
      entrysum++; // increment entrysum
    }
    

    Note: if entrysum always starts in 0 and increments by 1, you can simply use i from the loop instead.

    I hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败