[英]JQuery sum all data in specific table row
我有一個表在每個單元格中有輸入,除了一個(.total),它指定每個表行的總和。 當其中一個輸入被更改時,我希望該行的總和發生變化。 以下是我能夠得到的。 HTML:
<table>
<tr>
<td><input value = "100"/></td>
<td><input value = "100"/></td>
<td><input value = "100"/></td>
<td class="total">300</td>
</tr>
<tr>
<td><input value = "200"/></td>
<td><input value = "200"/></td>
<td><input value = "200"/></td>
<td class="total">600</td>
</tr>
</table>
現在為jQuery:
//add keyup handler
$(document).ready(function(){
$("input").each(function() {
$(this).keyup(function(){
newSum();
});
});
});
function newSum() {
var sum = 0;
var thisRow = $(this).closest('tr');
//iterate through each input and add to sum
$(thisRow).("td:not(.total) input:text").each(function() {
sum += this.value;
});
//change value of total
$(thisRow).(".total").html(sum);
}
任何有助於找出我做錯了什么的幫助都將不勝感激。 謝謝!
您的主要問題是在您的函數中使用this
。 如果將該函數的主體直接放在keyup
處理程序中,它應該可以工作。 但是this
在單獨聲明的函數的體將指,基本上,向其中它是一個方法的對象。 在上面的例子中,該對象將是window
。
嘗試這個:
$(document).ready(function(){
$("input").each(function() {
var that = this; // fix a reference to the <input> element selected
$(this).keyup(function(){
newSum.call(that); // pass in a context for newsum():
// call() redefines what "this" means
// so newSum() sees 'this' as the <input> element
});
});
});
你在newSum()
也有一些更膚淺的錯誤:
function newSum() {
var sum = 0;
var thisRow = $(this).closest('tr');
thisRow.find('td:not(.total) input:text').each( function(){
sum += parseFloat(this.value); // or parseInt(this.value,10) if appropriate
});
thisRow.find('td.total input:text').val(sum); // It is an <input>, right?
}
我可以找到一些問題和改進
parseInt
或parseFloat
將文本值parseInt
轉換為數字。 $(this).closest('tr')
不返回一組。 如果從事件處理程序傳遞事件對象,則可以將其更改為$(event.target).closest('tr')
。 原因是this
是在默認上下文中而不是在事件的上下文中進行評估。 $(selector).keyup(function() {})
綁定事件,您不需要使用.each
函數。 td:not(.total) input:text
在當前上下文中有點特定 - 它可以簡化。 解決方案有一些修改
function newSum(event) {
var sum = 0;
var thisRow = $(event.target).closest('tr');
thisRow.find("td input").each(function() {
sum += parseInt(this.value);
});
thisRow.find(".total").html(sum);
}
$("input").change(function(event) {
newSum(event);
});
如果newSum函數不必了解上下文,那么組合可能會更好
function sumInputsIn(inputs) {
var sum = 0;
inputs.each(function() {
sum += parseInt(this.value, 10);
});
return sum;
}
$("input").change(function(event) {
var row = $(event.target).closest('tr');
row.find('.total').html(sumInputsIn(row.find('td input')));
});
以下問題應該解決一些問題。
碼:
//add keyup handler
$(document).ready(function(){
$("input").each(function() {
$(this).keyup(function(){
newSum.call(this);
});
});
});
function newSum() {
var sum = 0;
var thisRow = $(this).closest('tr');
//iterate through each input and add to sum
$(thisRow).find("td:not(.total) input").each(function() {
sum += parseInt(this.value);
});
//change value of total
$(thisRow).find(".total").html(sum);
}
$(document).ready(function(){
$("input").keyup(function(){
var suma = 0;
$(this).parents("tr").children("td").children().each(function(){
suma+= parseInt($(this).val());
});
$(this).parent().parent().children(":last").text(suma);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.