2013-03-15 82 views
0

我一直在寻找一种方法来乘以两个动态创建的文本字段的值。我发现使用以下作为客户端JS功能。该total_inv_items_field是一个隐藏字段,增加的动态元素创建我使用该值来控制for循环:添加动态字段的值

function calcTotals() 
    { 
     var totalinvcitems = document.getElementById('total_inv_items_field').value; 
     var currinvcitem = 0; 
     var invoice_total_value = 0; 

     for (var invcitemcounter=0; invcitemcounter<totalinvcitems; invcitemcounter++) 
     { 
      currinvcitem = currinvcitem + 1; 

      var quantity = document.getElementById('qty' + currinvcitem).value; 
      var price = document.getElementById('cost' + currinvcitem).value; 
      var totallineitem = quantity * price; 
      document.getElementById('totallinefee' + currinvcitem).value = totallineitem; 
      invoice_total_value = invoice_total_value + totallineitem; 
     } 
     document.getElementById('invoice_total').value = invoice_total_value; 
    } 

每一件事对第二个为环工程的最后一道防线。动态创建的乘法运行正常。我无法弄清楚的问题是如何让变量“invoice_total_value”成为invoice_total表单组件的值。它从不更新。我尝试parseFloat被添加的数字,并尝试parseFloating invoice_total_value sum变量。我也认为它可能是一个类型转换问题,所以我尝试添加一个空('')字符串来将其转换为字符串,然后分配它。没有任何工作。我无法将其重新分配给静态文本字段。有任何想法吗?我得到一个偏头痛...;)

+1

你为什么包括PHP标签,没有在你的例子是有关 – 2013-03-15 03:28:11

+0

PHP你会像[jQuery](http://jquery.com/) – 2013-03-15 03:31:49

+0

那样使用类库吗?浏览器控制台中是否有任何错误 – 2013-03-15 03:35:08

回答

0

如果与ID invoice_total你的元素不是输入字段,然后执行:

document.getElementById('invoice_total').innerHTML = invoice_total_value; 
+0

好吧我觉得笨拙......我的问题是我的表单项的id字段有不同的引号'&“。 – user2172337 2013-03-15 04:05:46