javascript - 每当用户更改(即编辑)html 表中列中的值时,总计将自动重新计算?我怎样才能实现它?

标签 javascript jquery html css

enter image description here

我想构建一个这样的页面,其中 B 列应该是可编辑的,每当用户更改 B 列中的值时,总计应该相应地重新计算。

我想构建一个这样的页面,其中 B 列应该是可编辑的,每当用户更改 B 列中的值时,总计应该相应地重新计算。

我想构建一个这样的页面,其中 B 列应该是可编辑的,每当用户更改 B 列中的值时,总计应该相应地重新计算。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
table, td, th {
  border: 1px solid black;
}

table {
  border-collapse: collapse;

}

th {

}
</style>
</head>
<body>


<table id="myTable">
  <col width="130">
  <col width="80">
  <tr>
    <th>A</th>
    <th>B</th>

  </tr>
  <tr>
    <td>Rent</td>
    <td class="someClass" contenteditable="true">400</td>

  </tr>
  <tr>
    <td>Food</td>
    <td class="someClass" contenteditable="true">200</td>

  </tr>
  <tr>
    <td>Entertainment</td>
    <td class="someClass" contenteditable="true">100</td>

  </tr>
  <tr>
    <td>Transportation</td>
    <td class="someClass" contenteditable="true">50</td>

  </tr>

  <tr>
    <th>Total</th>
    <td class="someTotalClass">200</td>

  </tr>
</table>
<script>

 function sumOfColumns(){

        var totalQuantity = 0;
        var totalPrice = 0;
        $(".someClass").each(function(){
            totalQuantity += parseInt($(this).html());
            $(".someTotalClass").html(totalQuantity);
        });

        $(".classPrice").each(function(){
            totalPrice += parseInt($(this).html());
            $(".someTotalPrice").html(totalPrice);
        });
    }
sumOfColumns()


</script>


</body>
</html>

最佳答案

.blur() 将绑定(bind)一个处理程序以对 blur JavaScript 事件起作用——您只需将其添加到脚本标签中当前函数的下方就可以了。

通常我会说使用 .change() 但这似乎不适用于 contenteditable div。如果您可以更改为使用 <input>而不是 <div contenteditable="true">那么这可能是您的替代方案

<!DOCTYPE html>
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <style>
    table,
    td,
    th {
      border: 1px solid black;
    }
    
    table {
      border-collapse: collapse;
    }
    
    th {}
  </style>
</head>

<body>


  <table id="myTable">
    <col width="130">
    <col width="80">
    <tr>
      <th>A</th>
      <th>B</th>

    </tr>
    <tr>
      <td>Rent</td>
      <td class="someClass" contenteditable="true">400</td>

    </tr>
    <tr>
      <td>Food</td>
      <td class="someClass" contenteditable="true">200</td>

    </tr>
    <tr>
      <td>Entertainment</td>
      <td class="someClass" contenteditable="true">100</td>

    </tr>
    <tr>
      <td>Transportation</td>
      <td class="someClass" contenteditable="true">50</td>

    </tr>

    <tr>
      <th>Total</th>
      <td class="someTotalClass">200</td>

    </tr>
  </table>
  <script>
    function sumOfColumns() {
      var totalQuantity = 0;
      var totalPrice = 0;
      $(".someClass").each(function() {
        totalQuantity += parseInt($(this).html());
        $(".someTotalClass").html(totalQuantity);
      });

      $(".classPrice").each(function() {
        totalPrice += parseInt($(this).html());
        $(".someTotalPrice").html(totalPrice);
      });
    }

    sumOfColumns();

    $('.someClass').blur(function() {
      sumOfColumns()
    });
  </script>


</body>

</html>

关于javascript - 每当用户更改(即编辑)html 表中列中的值时,总计将自动重新计算?我怎样才能实现它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59021261/

相关文章:

javascript - 在 Jquery Ui Datepicker 中禁用今天之后的 future 日期

javascript - Google Visualization - 尝试格式化每个工具提示以通过格式化程序或查询显示百分比

javascript - JQuery.getJSON() 读取本地文件

javascript - 是否可以在浮点图的顶部放置一些东西?

javascript - 如何将 DRY 应用于此 JavaScript 对象?

javascript - HTML <select/> 如何防止点击父级的 div

html - 我怎样才能在浏览器之间始终将 css 居中?

html - 更改选中的单选按钮旁边的图像样式

javascript - 使用 angularjs 管理用户身份验证

javascript - 如何在 Backbone.Model.extend 中使用多个扩展?