【问题标题】:Dynamically recalculating total working hours of a generated HTML/JQuery Table动态重新计算生成的 HTML/JQuery 表的总工作时间
【发布时间】:2018-01-29 18:16:44
【问题描述】:

我有一个 HTML 表格,用户可以在其中输入他们的待机开始时间和待机结束时间,他们可以生成所述表格的新行以输入更多小时数。最后,当用户点击“计算”时,它会计算从一开始输入的补偿小时数,并显示出来。

我想弄清楚的是如何动态地重新计算删除一行时获得的总补偿小时数。

    var numRows = 2,
      ti = 5;
    var tableCount = 1;
    var index=1;
    
    window.standBy = function() {
      var sum = 0;
      $(".Standby").each(function(index, stand) {
        sum += parseFloat($(stand).val());
      })
    
      $(".grandtotal").val(sum)
    }
    
    function calculate() {
      var tr = $(this).closest('tr');
    
      var hours = parseInt($(".Time2", tr).val().split(':')[0], 10) - parseInt($(".Time1", tr).val().split(':')[0], 10);
      if (hours < 0) hours = 24 + hours;
      $(".Hours", tr).val(hours);
    
    if (hours <= 4) $(".Standby", tr).val("0.5");
      if (hours == 4) $(".Standby", tr).val("0.5");
      if (hours > 4 && hours < 8 ) $(".Standby", tr).val("1");
      if (hours == 8 ) $(".Standby", tr).val("1");
      if (hours > 8 && hours < 12) $(".Standby", tr).val("1.5");
      if (hours == 12 ) $(".Standby", tr).val("1.5");
      if (hours > 12 && hours < 16) $(".Standby", tr).val("2");
      if (hours == 16 ) $(".Standby", tr).val("2");
    	if (hours > 16 && hours < 20) $(".Standby", tr).val("2.5");
      if (hours == 20) $(".Standby", tr).val("2.5");
      if (hours > 20 && hours < 24) $(".Standby", tr).val("3");
      if (hours == 24) $(".Standby", tr).val("3");
      if (hours > 24 ) alert("You cannot exceed a 24 hour period.");
    
    
    
    }
    $('#table').on('change', ".Time1,.Time2", calculate);
    $('#table').find(".Time1").trigger('change')
    
    
    window.addTime = function() {
      tableCount++;
      $('#timeTable').clone().attr('id', "timeTable" + tableCount).appendTo('#table');
      $('#timeTable' + tableCount).find("input").val("");
      index++;
      $('#timeTable' + tableCount + ' .increment').html(tableCount);
      
    };
    
    
     $(document).on('click', 'button.removeTime', function () {
          	 var closestTable = $(this).closest('table');
           if(closestTable.attr('id') != "timeTable") {
           		closestTable.remove();
           }
           tableCount--;
    	   if (tableCount < 1) {
        tableCount = 1;
      }
    	   
    	   
           return false;
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>
    Time format is in 24h
    </h3>
    <h6><I>Example: If you want to type in "8 AM", the correct format would be: "8". <br> If you want to type in "8 PM", the correct format would be "20".</I></h6>
    
    <div id="table">
      <table id="timeTable" class="tg">
        <tr>
          <th class="tg-yw41"></th>
          <th class="tg-yw41"></th>
          <th class="tg-yw4l">Standby Start Time</th>
          <th class="tg-yw4l">Standby End Time</th>
          <th class="tg-yw4l">Hours in total</th>
          <th class="tg-yw4l">Compensatory Hours Earned</th>
        </tr>
        <tr>
        <td class="increment">1</td>
        <td class="tg-yw4l"><button class="removeTime">Remove Time</button></td>
       
             <td class="tg-yw4l">
            <input class="Time1" value="" placeholder="Enter your start time" />
          </td>
          <td class="tg-yw4l">
            <input class="Time2" value="" placeholder="Enter your end time" />
          </td>
          <td class="tg-yw4l">
            <input type="text" class="Hours" value="0" readonly="" />
          </td>
          <td class="tg-yw4l">
            <input type="text" class="Standby" value="0" readonly="" />
          </td>
        </tr>
      </table>
    </div>

    <hr>
    
    <button onclick="addTime();">Add Time</button>
    <br><br>
    
    <button onclick="standBy();">Calculate Total Compensatory Hours Earned</button>
    
    <caption>Total <abbr title="Compensatory">Comp</abbr> hours:</caption>&nbsp;
    <input class="grandtotal" value=""readonly="" />

我尝试将这个 sn-p:$('#table').on('change', ".Time1,.Time2", calculate); 放在我的“删除时间”功能中。好像没用。

我还有一个 JSFiddle 正在运行:https://jsfiddle.net/yL9q7gvc/

提前感谢大家的帮助!

【问题讨论】:

  • 你需要在你的jsfiddle的设置中添加jquery框架...

标签: javascript jquery html css


【解决方案1】:

只需将standBy() 函数放在removeTime 按钮单击事件中,如下所示。这是更新后的fiddler

    var numRows = 2,
      ti = 5;
    var tableCount = 1;
    var index=1;
    
    window.standBy = function() {
      var sum = 0;
      $(".Standby").each(function(index, stand) {
        sum += parseFloat($(stand).val());
      })
    
      $(".grandtotal").val(sum)
    }
    
    function calculate() {
      var tr = $(this).closest('tr');
    
      var hours = parseInt($(".Time2", tr).val().split(':')[0], 10) - parseInt($(".Time1", tr).val().split(':')[0], 10);
      if (hours < 0) hours = 24 + hours;
      $(".Hours", tr).val(hours);
    
    if (hours <= 4) $(".Standby", tr).val("0.5");
      if (hours == 4) $(".Standby", tr).val("0.5");
      if (hours > 4 && hours < 8 ) $(".Standby", tr).val("1");
      if (hours == 8 ) $(".Standby", tr).val("1");
      if (hours > 8 && hours < 12) $(".Standby", tr).val("1.5");
      if (hours == 12 ) $(".Standby", tr).val("1.5");
      if (hours > 12 && hours < 16) $(".Standby", tr).val("2");
      if (hours == 16 ) $(".Standby", tr).val("2");
    	if (hours > 16 && hours < 20) $(".Standby", tr).val("2.5");
      if (hours == 20) $(".Standby", tr).val("2.5");
      if (hours > 20 && hours < 24) $(".Standby", tr).val("3");
      if (hours == 24) $(".Standby", tr).val("3");
      if (hours > 24 ) alert("You cannot exceed a 24 hour period.");
    
    
    
    }
    $('#table').on('change', ".Time1,.Time2", calculate);
    $('#table').find(".Time1").trigger('change')
    
    
    window.addTime = function() {
      tableCount++;
      $('#timeTable').clone().attr('id', "timeTable" + tableCount).appendTo('#table');
      $('#timeTable' + tableCount).find("input").val("");
      index++;
      $('#timeTable' + tableCount + ' .increment').html(tableCount);
      
    };
    
    
     $(document).on('click', 'button.removeTime', function () {
          	 var closestTable = $(this).closest('table');
           if(closestTable.attr('id') != "timeTable") {
           		closestTable.remove();
           }
           tableCount--;
    	   if (tableCount < 1) {
        tableCount = 1;
      }
    	   
    	   standBy();
           return false;
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>
    Time format is in 24h
    </h3>
    <h6><I>Example: If you want to type in "8 AM", the correct format would be: "8". <br> If you want to type in "8 PM", the correct format would be "20".</I></h6>
    
    <div id="table">
      <table id="timeTable" class="tg">
        <tr>
          <th class="tg-yw41"></th>
          <th class="tg-yw41"></th>
          <th class="tg-yw4l">Standby Start Time</th>
          <th class="tg-yw4l">Standby End Time</th>
          <th class="tg-yw4l">Hours in total</th>
          <th class="tg-yw4l">Compensatory Hours Earned</th>
        </tr>
        <tr>
        <td class="increment">1</td>
        <td class="tg-yw4l"><button class="removeTime">Remove Time</button></td>
       
             <td class="tg-yw4l">
            <input class="Time1" value="" placeholder="Enter your start time" />
          </td>
          <td class="tg-yw4l">
            <input class="Time2" value="" placeholder="Enter your end time" />
          </td>
          <td class="tg-yw4l">
            <input type="text" class="Hours" value="0" readonly="" />
          </td>
          <td class="tg-yw4l">
            <input type="text" class="Standby" value="0" readonly="" />
          </td>
        </tr>
      </table>
    </div>

    <hr>
    
    <button onclick="addTime();">Add Time</button>
    <br><br>
    
    <button onclick="standBy();">Calculate Total Compensatory Hours Earned</button>
    
    <caption>Total <abbr title="Compensatory">Comp</abbr> hours:</caption>&nbsp;
    <input class="grandtotal" value=""readonly="" />

希望对你有帮助。

【讨论】:

  • 如果您要添加到您的答案,请删除不完整的答案。
  • @Snowmonkey 完成。在您编辑之前添加了第一个答案,忘记删除。
  • 非常感谢@RaJeshRiJo 的大力帮助!
猜你喜欢
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 2011-07-07
  • 1970-01-01
相关资源
最近更新 更多