Php 使用JQuery对多个表中的列值求和以获得总计
我使用php/mysql中的一个循环生成多个表,它们的结构相同,但值不同。我有一个脚本,可以对所有值求和并显示结果,但只有当值位于同一行时,它才起作用。我只需要对“precio”列求和 这是一个HTML。这是表格的结构:Php 使用JQuery对多个表中的列值求和以获得总计,php,jquery,Php,Jquery,我使用php/mysql中的一个循环生成多个表,它们的结构相同,但值不同。我有一个脚本,可以对所有值求和并显示结果,但只有当值位于同一行时,它才起作用。我只需要对“precio”列求和 这是一个HTML。这是表格的结构: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> <script type="text/javascript">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('tr').each(function () {
var sum = 0
$(this).find('.precio').each(function () {
var precio = $(this).text();
if (!isNaN(precio) && precio.length !== 0) {
sum += parseFloat(precio);
}
});
$('.total_prt', this).html(sum);
});
});
</script>
<table class="table table-striped">
<thead>
<tr>
<th width="70%">Descripción</th>
<th width="20%">Precio</th>
</tr>
</thead>
<tbody>
<tr>
<td>Display</td>
<td class="precio">1300</td>
</tr>
<tr>
<td>Keyboard</td>
<td class="precio">300</td>
</tr>
<tr>
<td><b>Total:</b></td>
<td class="total_prt"><b>$</b></td>
</tr>
</tbody>
</table>
<br>
<table class="table table-striped">
<thead>
<tr>
<th width="70%">Descripción</th>
<th width="20%">Precio</th>
</tr>
</thead>
<tbody>
<tr>
<td>HDD</td>
<td class="precio">400</td>
</tr>
<tr>
<td>System</td>
<td class="precio">425</td>
</tr>
<tr>
<td>Something</td>
<td class="precio">350</td>
</tr>
<tr>
<td><b>Total:</b></td>
<td class="total_prt"><b>$</b></td>
</tr>
</tbody>
</table>
$(文档).ready(函数(){
$('tr')。每个(函数(){
var总和=0
$(this).find('.precio').each(函数(){
var precio=$(this.text();
如果(!isNaN(precio)&&precio.length!==0){
总和+=浮点数(精度);
}
});
$('.total_prt',this).html(总和);
});
});
描述
普里西奥
展示
1300
键盘
300
总数:
$
描述
普里西奥
硬盘驱动器
400
系统
425
某物
350
总数:
$
您需要分别遍历所有表,并在表行的循环外部而不是循环内部初始化总和
$(文档).ready(函数(){
$('.table')。每个(函数(){
var总和=0
$(this).find('tr').each(function(){
$(this).find('.precio').each(function(){
var precio=$(this.text();
如果(!isNaN(precio)&&precio.length!==0){
总和+=浮点数(精度);
}
});
$('.total_prt',this).html(总和);
});
})
});代码>
描述
普里西奥
展示
1300
键盘
300
总数:
$
描述
普里西奥
硬盘驱动器
400
系统
425
某物
350
总数:
$
您拥有两个var sum=0
和$('.total_prt',this).html(sum)
位于错误的位置-您需要将它们移到$('tr')之外。每个(…)
循环 如果你这样做了,像这样的事情就会奏效
$(document).ready(function () {
$('.table').each(function (i, elem) {
var totalPrice = 0;
$(elem).find("td.precio").each(function(j, elem2) {
totalPrice += parseFloat($(elem2).html());
})
$(elem).find(".total_prt").html(totalPrice);
});
});
$('body')。查找('table')。每个(函数(){
var总和=0;
$(this).find('tr').each(function(){
$(this).find('td').each(function(){
if($(this).hasClass('precio')){
var precio=$(this.text();
如果($.isNumeric(precio)){
总和+=浮点数(精度);
}
}
});
});
$(this).find('td.total_prt').text(sum);
});代码>
描述
普里西奥
展示
1300
键盘
300
总数:
$
描述
普里西奥
硬盘驱动器
400
系统
425
某物
350
总数:
$
我所做的是循环通过total
元素,并将所有precio
元素文本相加以输出总数
$('.total_prt')。每个(函数(){
var总和=0;
$(this).parents('table').find('.precio').each(function(){
var flotted=parseFloat($(this.text());
如果(!isNaN(flotted))sum+=flotted;
});
$(this.html(sum);
});代码>
描述
普里西奥
展示
1300
键盘
300
总数:
$
描述
普里西奥
硬盘驱动器
400
系统
425
某物
350
总数:
$
最好、更简单。谢谢我把js放在最后。。。我想我早就知道了,但我犯了那个错误。。。