2017-03-07 41 views
0

这可能是一个简单的问题,但我该如何添加分钟以便他们变成小时,即如果您添加30分钟和30分钟30分钟,则会给出1小时30分钟的答案?我如何添加分钟,以便他们变成几小时?

到目前为止,我只是有几分钟总额:

  function findTotalHours(){ 
var arr = document.getElementsByName('hours'); 
var tot=0; 
for(var i=0;i<arr.length;i++){ 
    if(parseInt(arr[i].value)) 
     tot += parseInt(arr[i].value); 
} 
document.getElementById('totalHoursv').value = tot; 
     } 


function findTotalMins(){ 
    var arr = document.getElementsByName('minutes'); 
    var tot=0; 
    for(var i=0;i<arr.length;i++){ 
    if(parseInt(arr[i].value)) 
     tot += parseInt(arr[i].value); 
     } 
document.getElementById('totalMins').value = tot; 
    } 

我创建了一个JS小提琴显示哪些工作不正常的表...即它给90分钟作为答案

https://jsfiddle.net/Vicky1984/kLurp45y/9/

不胜感激任何意见,我完全新的JavaScript

感谢

回答

1
function findTotalHours(){ 
    var arr = document.getElementsByName('hours'); 
    var tot=0; 
    for(var i=0;i<arr.length;i++){ 
     if(parseInt(arr[i].value)) 
     tot += parseInt(arr[i].value); 
    } 

    return tot; 
} 
function findTotalMins(){ 
    var arr = document.getElementsByName('minutes'); 
    var tot=0; 
    for(var i=0;i<arr.length;i++){ 
    if(parseInt(arr[i].value)) 
     tot += parseInt(arr[i].value); 
    } 
    return tot; 
} 

function calculateAllocation(){ 
var mins = findTotalMins(); 
var hrs = findTotalHours(); 

hrs = hrs + (mins-mins%60)/60; 
mins = mins%60; 

document.getElementById('totalHoursv').value = hrs; 
document.getElementById('totalMins').value = mins; 
} 

在onblur事件调用calculateAllocation()两个小时和分钟输入字段

演示: https://jsfiddle.net/kLurp45y/19/

+0

真棒太感谢你了,非常酷 – Vicky12345

相关问题