Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用JQuery对多个表中的列值求和以获得总计_Php_Jquery - Fatal编程技术网

Php 使用JQuery对多个表中的列值求和以获得总计

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">

我使用php/mysql中的一个循环生成多个表,它们的结构相同,但值不同。我有一个脚本,可以对所有值求和并显示结果,但只有当值位于同一行时,它才起作用。我只需要对“precio”列求和

这是一个HTML。这是表格的结构:

<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放在最后。。。我想我早就知道了,但我犯了那个错误。。。