繁体   English   中英

如何将动态 javascript 输入字段的值相乘或相加

[英]How can i multiply or add the values of a dynamic javascript input field

输入字段由javascript生成。 无论我生成多少输入字段,我都想选择每个值......所以我可以使用这些值进行一些计算。

<form action="{{ route('addmorePost') }}" method="POST">
<div class="table-responsive">
                   <span id="result"></span>

                   <table class="table table-bordered">
                   <thead>
                  <tr>
                      <th width="55%">Sales Representative</th>
                      <th width="15%">Target per day</th>
                      <th width="15%">Monthly day</th>
                      <th width="10%"> <a href="#" class="btn btn-info addRow">+ </a> </th>
                  </tr>
                 </thead>
                 <tbody>
                    <tr> 
                        <td> 
              <input type="text" name="target[]" class="form-control target" id="target" oninput="calc()">
                       </td>

                       <td><span id="sum1" onchange="add()">0</span> </td>

                       <td> <a href="#" class="btn btn-danger remove">- </a> </td>
                      </tr>
                 </tbody>
                 <tfoot>


                    <tr>
                       <th>  
                       </th>
                       <th> Total</th>
                       <th><span id="sumx">0</span> </th> 
                     </tr>

                   </tfoot>


                   <script>                
                      function add() {

                      var ven1 = document.getElementById('sum1').innerHTML;
                      var valuey = document.getElementById('result1').innerHTML || 0; 
                      console.log(ven1);
                      console.log(valuey);
                      document.getElementById('sumx').innerHTML = parseInt(ven1) + parseInt(valuey);

                    }
                   </script>




             </table>

              </button>

            </form> 

下面是用于生成INPUT FIELDSJAVASCRIPT

<script type="text/javascript">

  $(document).ready(function(){      
  var postURL = "http://localhost/bcwater/public/addmore";
  var i=1; 

  $('.addRow').on('click', function(){
    i++;
    addRow();
  });

  function addRow(){
     var tr = '<tr class="dynamic-added">'+
                '<td>'+
                '</td>'+
                '<td><input type="text" name="target[]" id="target2" class="form-control" oninput="calc()" /> </td>'+
                '<td><span id="result1">0</span> </td>'+
                '<td> <a href="#" class="btn btn-danger remove">- </a> </td>'+

              '</tr>';
          $('tbody').append(tr);
  }; 

  $('tbody').on('click','.remove', function(){
    $(this).parent().parent().remove();
  });



});  

这是 CODEI 尝试获取值,将它们乘以 26 并在 sum1 上显示答案

    <script>


    function calc(){       
    var value1 = document.getElementById('target').value || 0;
    document.getElementById('sum1').innerHTML = parseInt(value1 * 26);

    var value2 = document.getElementById('target2').value; 
    document.getElementById('result1').innerHTML = parseInt(value2 * 26);


    var ven1 = document.getElementById('sum1').innerHTML;
    var valuey = document.getElementById('result1').innerHTML || 0; 
    console.log(ven1);
    console.log(valuey);
    document.getElementById("products").disabled = false;
    document.getElementById('sumx').innerHTML = parseInt(ven1) + parseInt(valuey);

    }

    </script>

请帮忙......我做错了什么

每次调用addRow()从您的代码中,ID 始终为target2 要自动更改 ID,有一个保存当前 ID 的变量并连接字符串,您可以使用var i

$(document).ready(function(){      
  var postURL = "http://localhost/bcwater/public/addmore";
  var i=1; 

  $('.addRow').on('click', function(){
    i++;
    addRow(i);
  });

  function addRow(i){
     var tr = '<tr class="dynamic-added">'+
            '<td>'+
            '</td>'+
            '<td><input type="text" name="target[]" id="target' + i + '" class="form-control" oninput="calc()" /> </td>'+
            '<td><span id="result' + i + '">0</span> </td>'+ //This is modified to attach the current index to result
            '<td> <a href="#" class="btn btn-danger remove">- </a> </td>'+

          '</tr>';
      $('tbody').append(tr);
  }; 

  $('tbody').on('click','.remove', function(){
     $(this).parent().parent().remove();
  });
});

i变量应始终保存添加的最后一行的索引

要乘以您必须使用相同i变量的值,function calc()应该如下所示,我建议您将i重命名为描述性的名称:

function calc(){       
    var value1 = document.getElementById('target').value || 0;
    document.getElementById('sum1').innerHTML = parseInt(value1 * 26);

    for(var j = 1; j <= i; j++) {
        var value2 = document.getElementById('target' + j).value; 
        document.getElementById('result' + j).innerHTML = parseInt(value2 * 26); // This is to target the result span that corresponds to the input value
    }


    var ven1 = document.getElementById('sum1').innerHTML;
    var valuey = document.getElementById('result1').innerHTML || 0; 
    console.log(ven1);
    console.log(valuey);
    document.getElementById("products").disabled = false;
    document.getElementById('sumx').innerHTML = parseInt(ven1) + parseInt(valuey);

}

谢谢你..我能够这样做......在我像这样 id="target' + i + '" 和 span 一样创建 id 的值之后

所以我使用下面的代码手动获取值......不是最好的选择,但它解决了我的问题,因为我不需要三个以上的输入字段

        function calc(){     

        var value1 = document.getElementById('target').value || 0;
        document.getElementById('sum1').innerHTML = parseInt(value1 * 26);

        var value2 = document.getElementById('target2').value; 
        document.getElementById('result2').innerHTML = parseInt(value2 * 26);

        document.getElementById("products").disabled = false;


        var value2 = document.getElementById('target3').value; 
        document.getElementById('result3').innerHTML = parseInt(value2 * 26);




        var ven1 = document.getElementById('sum1').innerHTML || 0;
        var valuey = document.getElementById('result2').innerHTML || 0; 

        var valuenew = document.getElementById('result3').innerHTML || 0; 



        document.getElementById('sumx').innerHTML = parseInt(ven1) + parseInt(valuey) + parseInt(valuenew);

        }

        </script>

暂无
暂无

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

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