Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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
Javascript jQuery单击时HTML列的小计_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery单击时HTML列的小计

Javascript jQuery单击时HTML列的小计,javascript,jquery,html,Javascript,Jquery,Html,在以下结构中: <input type="text" class="form-control" id="price" placeholder="Price"> <input type="text" class="form-control" id="sum_amount" placeholder="Total amount"> <input type="text" class="form-control" id="sum_total" placeholde

在以下结构中:

<input type="text" class="form-control" id="price" placeholder="Price">

<input type="text" class="form-control" id="sum_amount" placeholder="Total amount">    

<input type="text" class="form-control" id="sum_total" placeholder="Grand total">      

<table class="table" id="tab1">
                        <thead>
                            <tr>
                                <th>Price</th>
                                <th>Amount</th>
                                <th>Total</th>
                                <th>

                                </th>
                            </tr>
                        </thead>
                        <tbody class="tableSell">
                            <tr>
                                <td>625</td>
                                <td>1</td>
                                <td>625</td>
                            </tr>
                            <tr>
                                <td>638</td>
                                <td>2.5</td>
                                <td>1595</td>
                            </tr>
                            <tr>
                                <td>640</td>
                                <td>4</td>
                                <td>2560</td>
                            </tr>
                        </tbody>
                    </table>
但我不知道如何发展小计


工作区:

已编辑:固定为
parseFloat()
,并添加了JSFIDLE

尝试将总数初始化为行中的当前总数。然后循环回到前面的所有行,并将这些行添加到总计中,如下所示(这将进入
。单击()
函数):

您可能还希望为单击事件指定选择器,使其更像:

$('#tab1 > tbody > tr')

这将防止分析错误,因为您的
thead
部分中的标题行不会与此一起被选中。

编辑的:修复为
parseFloat()
,并添加了JSFIDLE

$(document).ready(function(){
  $("#tab1 tr").click(function(){
      var amount = parseFloat($(this).find(':eq(1)').text());
      var price = $(this).find(':first-child').text();
      var total = 0;

      //add one to amount and get this rows total
      amount += 1;
      total = amount * price;

      var grandTotal = 0;
      var sum_amount = 0;  

 //update the row 
 $(this).find(':eq(1)').text(amount);
 $(this).find(':eq(2)').text(total); 

  var count = 0;

  //now loop through the rows and get the totals - the count is to ignore the first row as it is table headings
  $.each($('table tr'), function( i, val ) {

      if (count > 0){
          sum_amount += parseFloat($(val).find(':eq(1)').text());
          grandTotal += parseFloat($(val).find(':eq(2)').text());
      }

      count ++;
尝试将总数初始化为行中的当前总数。然后循环回到前面的所有行,并将这些行添加到总计中,如下所示(这将进入
。单击()
函数):

您可能还希望为单击事件指定选择器,使其更像:

$('#tab1 > tbody > tr')

这将防止分析错误,因为您的
thead
部分中的标题行不会与此一起被选中。

编辑的:修复为
parseFloat()
,并添加了JSFIDLE

$(document).ready(function(){
  $("#tab1 tr").click(function(){
      var amount = parseFloat($(this).find(':eq(1)').text());
      var price = $(this).find(':first-child').text();
      var total = 0;

      //add one to amount and get this rows total
      amount += 1;
      total = amount * price;

      var grandTotal = 0;
      var sum_amount = 0;  

 //update the row 
 $(this).find(':eq(1)').text(amount);
 $(this).find(':eq(2)').text(total); 

  var count = 0;

  //now loop through the rows and get the totals - the count is to ignore the first row as it is table headings
  $.each($('table tr'), function( i, val ) {

      if (count > 0){
          sum_amount += parseFloat($(val).find(':eq(1)').text());
          grandTotal += parseFloat($(val).find(':eq(2)').text());
      }

      count ++;
尝试将总数初始化为行中的当前总数。然后循环回到前面的所有行,并将这些行添加到总计中,如下所示(这将进入
。单击()
函数):

您可能还希望为单击事件指定选择器,使其更像:

$('#tab1 > tbody > tr')

这将防止分析错误,因为您的
thead
部分中的标题行不会与此一起被选中。

编辑的:修复为
parseFloat()
,并添加了JSFIDLE

$(document).ready(function(){
  $("#tab1 tr").click(function(){
      var amount = parseFloat($(this).find(':eq(1)').text());
      var price = $(this).find(':first-child').text();
      var total = 0;

      //add one to amount and get this rows total
      amount += 1;
      total = amount * price;

      var grandTotal = 0;
      var sum_amount = 0;  

 //update the row 
 $(this).find(':eq(1)').text(amount);
 $(this).find(':eq(2)').text(total); 

  var count = 0;

  //now loop through the rows and get the totals - the count is to ignore the first row as it is table headings
  $.each($('table tr'), function( i, val ) {

      if (count > 0){
          sum_amount += parseFloat($(val).find(':eq(1)').text());
          grandTotal += parseFloat($(val).find(':eq(2)').text());
      }

      count ++;
尝试将总数初始化为行中的当前总数。然后循环回到前面的所有行,并将这些行添加到总计中,如下所示(这将进入
。单击()
函数):

您可能还希望为单击事件指定选择器,使其更像:

$('#tab1 > tbody > tr')
这将防止分析错误,因为您的
thead
部分中的标题行不会与此一起选择

$(document).ready(function(){
  $("#tab1 tr").click(function(){
      var amount = parseFloat($(this).find(':eq(1)').text());
      var price = $(this).find(':first-child').text();
      var total = 0;

      //add one to amount and get this rows total
      amount += 1;
      total = amount * price;

      var grandTotal = 0;
      var sum_amount = 0;  

 //update the row 
 $(this).find(':eq(1)').text(amount);
 $(this).find(':eq(2)').text(total); 

  var count = 0;

  //now loop through the rows and get the totals - the count is to ignore the first row as it is table headings
  $.each($('table tr'), function( i, val ) {

      if (count > 0){
          sum_amount += parseFloat($(val).find(':eq(1)').text());
          grandTotal += parseFloat($(val).find(':eq(2)').text());
      }

      count ++;
}))

})); });

}))

})); });

}))

})); });

}))

})); });



我认为他们不希望它像那样递归地添加,但我可能又错了。问题一开始不是很清楚。同意不清楚-希望我们中的一个人能给出他们想要的答案:-是的,不应该递归添加,抱歉,如果问题不清楚。没问题-2220应该猜到,因为3.5行是2233:-P,但是我希望里面有一些有用的东西。好运气谢谢你,当然它是有用的,我把它标记为:)我认为他们不希望它像那样递归地添加,但我可能又错了。问题一开始不是很清楚。同意不清楚-希望我们中的一个人能给出他们想要的答案:-是的,不应该递归添加,抱歉,如果问题不清楚。没问题-2220应该猜到,因为3.5行是2233:-P,但是我希望里面有一些有用的东西。好运气谢谢你,当然它是有用的,我把它标记为:)我认为他们不希望它像那样递归地添加,但我可能又错了。问题一开始不是很清楚。同意不清楚-希望我们中的一个人能给出他们想要的答案:-是的,不应该递归添加,抱歉,如果问题不清楚。没问题-2220应该猜到,因为3.5行是2233:-P,但是我希望里面有一些有用的东西。好运气谢谢你,当然它是有用的,我把它标记为:)我认为他们不希望它像那样递归地添加,但我可能又错了。问题一开始不是很清楚。同意不清楚-希望我们中的一个人能给出他们想要的答案:-是的,不应该递归添加,抱歉,如果问题不清楚。没问题-2220应该猜到,因为3.5行是2233:-P,但是我希望里面有一些有用的东西。祝你好运,谢谢你,它当然有用,我把它标记为:)