2011-04-17 71 views
1

我有一個表格,其中包括具有下面基本結構的輸入文本字段。我無法構建一個迭代表中所有行的函數,並總結以BFObel開頭的輸入字段的所有值,其中以BFOkto開頭的字段的值相同。因此,以價值爲1111總和低於基本的例子是,2000和1112的值的總和將是3000然後,每個總和將被寫入到inputfield用id field1111,field1112等等jquery:求和輸入文本字段

<table> 
    <tr id="BFOrow1"> 
    <td><input type="text" id="BFOtxt1" value="text"/></td> 
    <td><input type="text" id="BFOkto1" value="1111" /></td> 
    <td><input type="text" id="BFObel1" value="1000" /></td> 
    </tr> 
    <tr id="BFOrow2"> 
    <td><input type="text" id="BFOtxt2" value="text"/></td> 
    <td><input type="text" id="BFOkto2" value="1111" /></td> 
    <td><input type="text" id="BFObel2" value="1000" /></td> 
    </tr> 
    <tr id="BFOrow3"> 
    <td><input type="text" id="BFOtxt3" value="text"/></td> 
    <td><input type="text" id="BFOkto3" value="1112" /></td> 
    <td><input type="text" id="BFObel3" value="1000" /></td> 
    </tr> 
    <tr id="BFOrow4"> 
    <td><input type="text" id="BFOtxt4" value="text"/></td> 
    <td><input type="text" id="BFOkto4" value="1112" /></td> 
    <td><input type="text" id="BFObel4" value="1000" /></td> 
    </tr> 
    <tr id="BFOrow5"> 
    <td><input type="text" id="BFOtxt5" value="text"/></td> 
    <td><input type="text" id="BFOkto5" value="1112" /></td> 
    <td><input type="text" id="BFObel5" value="1000" /></td> 
    </tr> 
</table> 
+0

您需要更好地說出您的問題併發布一些html – Calum 2011-04-17 19:34:00

+1

由於您正在計算幾筆款項,您打算如何處理結果?他們應該如在數組中返回,還是作爲屬性/數據存儲在行本身中? – 2011-04-17 19:38:25

+0

每個總和將被寫入另一個輸入字段,其ID爲1111或1112 – 2011-04-17 19:40:59

回答

0
var sum1111 = 0; 

$('input[value="1111"]').each(function() { 
    var ordinal = $(this).attr('id').replace('BFOkto', ''); 
    sum1111 += parseInt($('#BFObel' + ordinal).val()); 
}); 

在結束時,sum1111應等於2000

對於重用,包裹邏輯的函數:

function getSum(BFOkto) { 
    var sum = 0; 
    var ordinal = null; 

    $('input[value="' + BFOkto + '"]').each(function() { 
     ordinal = $(this).attr('id').replace('BFOkto', ''); 
     sum += parseInt($('#BFObel' + ordinal).val()); 
    }); 

    return sum; 
} 

然後調用:

getSum('1111'); 
getSum('1112'); 
+0

在我的情況下,值從開始就不知道(取決於用戶輸入),所以我需要一個可用於未知值/選擇器的函數。 – 2011-04-17 19:55:30

+0

ok,然後用getSum($('#BFOkto1').val())替換getSum('1111') – Kon 2011-04-17 19:57:13

+0

好像它也可以工作。感謝您的好解決方案! – 2011-04-17 20:19:12

2

你要使用對象文本來跟蹤廣告效果和"attribute starts with" selector查找的文本輸入:

var accumulator = { }; 
$('table input[id^=BFOkto]').each(function() { 
    var sum_id = this.id.replace(/^BFOkto/, 'BFObel'); 
    if(!accumulator[this.value]) 
     accumulator[this.value] = 0; 
    accumulator[this.value] += parseInt($('#' + sum_id).val(), 10); 
}); 
// accumulator now has your results. 

第二個參數不要忘了parseInt(),這樣你就不會得到由具有前導零的值絆倒(看起來像沒有指定基數的八進制)。

例如:http://jsfiddle.net/ambiguous/QAqsQ/(您需要在瀏覽器中使用開放式JavaScript控制檯運行此操作以查看生成的accumulator)。

+0

+1,很好地迴避了問題的模糊性:) – 2011-04-17 19:59:51

+0

似乎很好地工作。將進一步嘗試。 – 2011-04-17 20:22:19

0

一種不同的方法:找到具有前綴BFOkto所有輸入字段,對於每個,找到具有前綴BFObel輸入共享相同的父和累加其值

ref = $("table td input[id^=BFOkto]"); 

var sums = new Object(); 
ref.each(function(){ 
    val = parseInt($(this).closest('tr').find("td input[id^=BFObel]").val(), 10); 
    property = 'i'+ this.value; 
    sums[property] = (sums[property] || 0) + val; 
}); 

alert(sums['i1111']); 
alert(sums['i1112']); 

總和將與屬性的對象

i1111 = 2000 
i1112 = 3000 

儘管JavaScript允許它,它是最好不要使用純數字屬性對對象(關聯數組),因此第i前綴

釷e運行示例如下: http://jsfiddle.net/TbSau/1/