我一直在尋找一種方法來乘以兩個動態創建的文本字段的值。我發現使用以下作爲客戶端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變量。我也認爲它可能是一個類型轉換問題,所以我嘗試添加一個空('')字符串來將其轉換爲字符串,然後分配它。沒有任何工作。我無法將其重新分配給靜態文本字段。有任何想法嗎?我得到一個偏頭痛...;)
你爲什麼包括PHP標籤,沒有在你的例子是有關 – 2013-03-15 03:28:11
PHP你會像[jQuery](http://jquery.com/) – 2013-03-15 03:31:49
那樣使用類庫嗎?瀏覽器控制檯中是否有任何錯誤 – 2013-03-15 03:35:08