使用momentjs在多个表中总和时间

时间:2016-08-22 03:33:19

标签: javascript momentjs

我试图将时间与momentjs相加。

这是我的JavaScript函数:

function all_time()
 {
    $(".aaa").each(function() {
      $this = $(this);
        var total = moment().startOf('day');
            $this.find('.jmljam').each (function() {
            var value = $this.find(this).val();
            var thisMoment = moment(value, 'HH:mm', true);
                if(thisMoment.isValid()){
                 total.add(thisMoment);
                  $this.find(".totalseluruhjam").val(total.format("HH:mm"));
                }
            });
        });
 }

这是我的HTML表格

<form method="POST" action="http://localhost:84/payroll2/dinas_audit/claim/1105321/AD0000002">        

    <table class="aaa table">
    <thead>
        <tr>
            <td> No </td>
            <td> Full Name </td>
            <td> Attendance Date </td>
            <td> In Time </td>
            <td> Out Time </td>
            <td> Waktu Lebih </td>
        </tr>
    </thead>
    <tbody>
        <tr>
    <td>1</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-01</td>
    <td class="jammasuk">
        07:00:00    </td>                                                                           
    <td class="jampulang">
    23:00:00    </td>
    <td class="lebih">08:00</td>

    </tr>
         <tr>
    <td>2</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-02</td>
    <td class="jammasuk">
        -    </td>                                                                          
    <td class="jampulang">
    -    </td>
    <td class="lebih">00:00</td>

    </tr>
         <tr>
    <td>3</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-03</td>
    <td class="jammasuk">
        -    </td>                                                                          
    <td class="jampulang">
    -    </td>
    <td class="lebih">00:00</td>

    </tr>

             <tr>

            <td colspan="4"> </td>
            <td>Total Jam Lebih</td>
            <td>
             <input name="jamlebih" type="text" readonly disabled class="jmljam" />
            </td>
        </tr>
         <tr>
            <td colspan="4"> </td>
            <td>Ro Yang DiDapatkan</td>
             <td>
             <input name="dapetro" type="text" readonly class="ronya" />
             </td>
        </tr>
            </tbody>
</table>

    <table class="aaa table">
    <thead>
        <tr>
            <td> No </td>
            <td> Full Name </td>
            <td> Attendance Date </td>
            <td> In Time </td>
            <td> Out Time </td>
            <td> Waktu Lebih </td>
        </tr>
    </thead>
    <tbody>
        <tr>
    <td>4</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-04</td>
    <td class="jammasuk">
        09:00:00    </td>                                                                           
    <td class="jampulang">
    18:21:00    </td>
    <td class="lebih">01:21</td>

    </tr>
         <tr>
    <td>5</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-05</td>
    <td class="jammasuk">
        09:00:00    </td>                                                                           
    <td class="jampulang">
    20:21:00    </td>
    <td class="lebih">03:21</td>

    </tr>
         <tr>
    <td>6</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-06</td>
    <td class="jammasuk">
        09:00:00    </td>                                                                           
    <td class="jampulang">
    21:21:00    </td>
    <td class="lebih">04:21</td>

    </tr>
         <tr>
    <td>7</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-07</td>
    <td class="jammasuk">
        -    </td>                                                                          
    <td class="jampulang">
    -    </td>
    <td class="lebih">00:00</td>

    </tr>
         <tr>
    <td>8</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-08</td>
    <td class="jammasuk">
        -    </td>                                                                          
    <td class="jampulang">
    -    </td>
    <td class="lebih">00:00</td>

    </tr>
         <tr>
    <td>9</td>
    <td>[1105321] - AL SHIFAUL LATIFAH</td>
    <td>2016-09-09</td>
    <td class="jammasuk">
        -    </td>                                                                          
    <td class="jampulang">
    -    </td>
    <td class="lebih">00:00</td>

    </tr>

             <tr>

            <td colspan="4"> </td>
            <td>Total Jam Lebih</td>
            <td>
             <input name="jamlebih" type="text" readonly disabled class="jmljam" />
            </td>
        </tr>
         <tr>
            <td colspan="4"> </td>
            <td>Ro Yang DiDapatkan</td>
             <td>
             <input name="dapetro" type="text" readonly class="ronya" />
             </td>
        </tr>
            </tbody>
</table>

 <table class="table">
  <tr>
      <td> Total Jam Lebih : </td><td><input type="text" readonly="readonly" class='totalseluruhjam'/></td>
    </tr>
    <tr>
      <td> Total RO Yang Di Dapat : </td><td><input type="text" readonly="readonly" class='totalseluruhro'/></td>
    </tr>
 </table>  
              </div>



                      <div class="box-footer">
                        <input value='submit' type='submit' class='btn btn-success pull-right claim'>                       </div>            
              </div>

</div>
        </form>  

从上面可以看出。我有多个表。我可以用这个js

来总结.totalseluruhro`
function all_ro()
 {
     var sum=0;
    $(".aaa").each(function() {
        $this = $(this);
        sum += parseFloat($this.find('.ronya').val());
        $('.totalseluruhro').val(sum);
    })
 }

但现在我尝试对每个.jmljam求和,然后将结果设置为.totalseluruhjam。我试图将jmljamfunction all_time()相加,但它不起作用。我无法从浏览器控制台看到任何错误。那么,如何总结.jmljam

请查看我的小提琴https://jsfiddle.net/s9wfh9ye/23/

1 个答案:

答案 0 :(得分:1)

用这个来拍摄。

function all_time()
     {
        var total = moment().startOf('day');
        $(".aaa").each(function() {
          $this = $(this);

                $this.find('.jmljam').each (function() {
                var value = $this.find(this).val();
                var thisMoment = moment(value, 'HH:mm', true);

                if(thisMoment.isValid()){

                    total.add(thisMoment);

                }
            });
        });
        $(".totalseluruhjam").val(total.format("HH:mm"));
     }

这是更新的小提琴https://jsfiddle.net/s9wfh9ye/24/