Javascript 更改时多行之间的时差和求和

Javascript 更改时多行之间的时差和求和,javascript,html,Javascript,Html,我正在尝试编写一个可以执行以下操作的代码: 计算一行上两个type=“time”元素之间的时间差(以HH:MM格式) 对文本输入中的总差异(以HH:MM格式)求和 我已经设法创建了一个由onchange事件触发的函数,但是我的函数只考虑输入的第一个值,这意味着当您更新计时时,将重新计算差值,并且总数将是错误的 下面是我计算第一行的JavaScript代码: function CalTime0() { var timeOfCall = $('#timefrom0').val(),

我正在尝试编写一个可以执行以下操作的代码:

  • 计算一行上两个
    type=“time”
    元素之间的时间差(以HH:MM格式)
  • 对文本输入中的总差异(以HH:MM格式)求和
  • 我已经设法创建了一个由
    onchange
    事件触发的函数,但是我的函数只考虑输入的第一个值,这意味着当您更新计时时,将重新计算差值,并且总数将是错误的

    下面是我计算第一行的JavaScript代码:

    function CalTime0() {
        var timeOfCall = $('#timefrom0').val(),
        timeOfResponse = $('#timeto0').val(),
        hours = timeOfResponse.split(':')[0] - timeOfCall.split(':')[0],
        minutes = timeOfResponse.split(':')[1] - timeOfCall.split(':')[1],
        total = $('#tbtotal').val(),
        tothours = total.split(':')[0],
        totminutes = total.split(':')[1];               
        minutes = minutes.toString().length<2?'0'+minutes:minutes;
        totminutes = totminutes.toString().length<2?'0'+totminutes:totminutes;
    
        if(minutes<0) { 
            hours--;
            minutes = 60 + minutes;
        }
    
        hours = hours.toString().length<2?'0'+hours:hours;
        tothours = tothours.toString().length<2?'0'+tothours:tothours;
        tothours = parseInt(tothours) + parseInt(hours);
        totminutes = parseInt(totminutes) + parseInt(minutes);
    
        if(totminutes >= 60) { 
            tothours++;
            totminutes = totminutes - 60;
        }
    
        $('#total0').val(hours + ':' + minutes);    
        $('#tbtotal').val(tothours + ':' + totminutes); 
    }
    
    函数CalTime0(){
    var timeOfCall=$('#timefrom0').val(),
    响应时间=$('#timeto0').val(),
    小时=响应时间.split(':')[0]-调用时间.split(':')[0],
    分钟=响应时间.split(':')[1]-调用时间.split(':')[1],
    总计=$('#tbtotal').val(),
    tothours=total.split(“:”)[0],
    totminutes=total.split(“:”)[1];
    
    minutes=minutes.toString().length作为建议,要更轻松地计算日期差异,请使用moment.js。日期差异计算应可替换为以下内容,即:

    moment(timeOfResponse).diff(moment(timeOfCall))
    

    moment.js diff documentation:

    解决方案是从
    #tbtotal
    字段所代表的时间减去
    #total0
    字段所代表的时间。然后计算新的
    #total0
    字段,并再次将该时间添加到
    #tbtotal
    中。这样,
    #tbtotal
    中显示的时间始终是正确的矩形

    另一个问题似乎是如何对所有行执行此操作,而不仅仅是对这一行。您可以使用
    this
    关键字来确定触发更改事件的元素。从中可以确定其他元素是什么。为此,我将字段重命名为
    timer0
    timeto0
    ,因此它们是相等的双耳长度

    我自由地将所有时间转换为秒,并以这种方式操纵它们。脚本中的注释应该代表他们自己

    function updateTotals() {
      //Get num part of the id from current set
      //Cheated a bit with the id names ;-)
      var num = $(this).attr('id').substr( 6 );
    
      //Get the time from each and every one of them
      var tfrom = $('#timefr' + num ).val().split(':');
      var tto = $('#timeto' + num ).val().split(':');
      var currtotal = $('#total' + num ).val().split(':');
      var grandtotal = $('#tbtotal').val().split(':');
    
      //Convert to seconds and do the calculations the easy way
      var diff = (tto[0] - tfrom[0]) * 3600 + (tto[1] - tfrom[1]) * 60;
      var totalsec = currtotal[0] * 3600 + currtotal[1] * 60;
      var grandtotalsec = grandtotal[0] * 3600 + grandtotal[1] * 60;
    
      //If the result is negative, we can't do anything sensible. Use 0 instead.
      if( diff < 0 ) {
        diff = 0;
      }
    
      //Substract what we calculated last time
      grandtotalsec -= totalsec;
      //Then add the current diff
      grandtotalsec += diff;
    
      //Convert diff (or total) into human readable form
      var hours = Math.floor( diff / 3600 );
      diff = diff % 3600;
      var minutes = Math.floor( diff / 60 );
    
      hours = (hours < 10) ? "0" + hours.toString() : hours.toString();
      minutes = (minutes < 10) ? "0" + minutes.toString() : minutes.toString();
    
    
      //Convert grandtotal into human readable form
      var grandtotalhours = Math.floor( grandtotalsec / 3600 );
      grandtotalsec = grandtotalsec % 3600;
      var grandtotalminutes = Math.floor( grandtotalsec / 60 );
    
      grandtotalhours = (grandtotalhours < 10) ? "0" + grandtotalhours.toString() : grandtotalhours.toString();
      grandtotalminutes = (grandtotalminutes < 10) ? "0" + grandtotalminutes.toString() : grandtotalminutes.toString();  
    
      //Put them in the fields
      $( '#total' + num ).val( hours + ":" + minutes );
      $( '#tbtotal' ).val( grandtotalhours + ":" + grandtotalminutes );
    }
    
    函数updateTotals(){
    //从当前集合中获取id的num部分
    //在id名称上有点作弊;-)
    var num=$(this.attr('id').substr(6);
    //从他们每个人那里得到时间
    var tfrom=$('#timefr'+num).val().split(':');
    var tto=$('#timeto'+num).val().split(':');
    var currtotal=$('#total'+num).val().split(':');
    var grandtotal=$('#tbtotal').val().split(':');
    //转换为秒,并以简单的方式进行计算
    变量差异=(tto[0]-tfrom[0])*3600+(tto[1]-tfrom[1])*60;
    var totalsec=currtotal[0]*3600+currtotal[1]*60;
    var grandtotalsec=grandtotal[0]*3600+grandtotal[1]*60;
    //如果结果为负,则无法执行任何合理的操作。请使用0。
    如果(差异<0){
    差异=0;
    }
    //减去我们上次计算的数值
    grandtotalsec-=totalsec;
    //然后添加当前的差异
    grandtotalsec+=diff;
    //将差异(或总计)转换为人类可读的形式
    var小时=数学楼层(差异/3600);
    差异=差异%3600;
    var分钟=数学楼层(差异/60);
    小时=(小时<10)?“0”+小时.toString():小时.toString();
    分钟=(分钟<10)?“0”+分钟.toString():分钟.toString();
    //将grandtotal转换为人类可读的形式
    var grandtotalhours=数学楼层(grandtotalsec/3600);
    grandtotalsec=grandtotalsec%3600;
    var grandtotalminutes=数学楼层(grandtotalsec/60);
    grandtotalhours=(grandtotalhours<10)?“0”+grandtotalhours.toString():grandtotalhours.toString();
    grandtotalminutes=(grandtotalminutes<10)?“0”+grandtotalminutes.toString():grandtotalminutes.toString();
    //把它们放在地里
    $('#total'+num).val(小时+:“+min);
    $('#tbtotal').val(grandtotalhours+“:“+grandtotalminutes);
    }
    

    可以找到一个例子。

    如果你在上面发布了一些东西,请花一些时间为你的代码获得正确的缩进。正确缩进的代码更容易阅读。我只是缩进了你的代码,看起来有一大块代码不见了。我想这不会运行。谢谢@Sumurai8,我已经修改了代码。你能不能只删减它原来在
    #total0
    中的值?如果有10行,并且每行我使用不同的函数,那么您的建议是添加更多函数来选择当前值并更新它?