我正在尝试建立一个基于网站"电子表格"这将用于计算一组"员工"中使用的小时/人工。我能够弄清楚如何计算其中一个"员工"的小时数,但我无法弄清楚如何为其他"员工"使用相同的公式/功能。 。 This jsfiddle具有我迄今为止能够整理的基本结构。
<form>
<table id="actual" cellspacing="0px">
<tr>
<th>Name</th>
<th>Start Time</th>
<th>End Time</th>
<th>
Total Time
<br/> Worked
</th>
</tr>
<tr>
<th>Greg Weiland</th>
<td>
<input class="Time1" type="time">
</td>
<td>
<input class="Time2" type="time">
</td>
<td>
<input class="Hours">
</td>
</tr>
<tr>
<th>Alicia Hawly</th>
<td>
<input class="Time1" type="time">
</td>
<td>
<input class="Time2" type="time">
</td>
<td>
<input class="Hours">
</td>
</tr>
<tr>
<th>Charlen Connoly</th>
<td>
<input type="time">
</td>
<td>
<input type="time">
</td>
<td> </td>
</tr>
<tr>
<th>Dakota Giles</th>
<td>
<input type="time">
</td>
<td>
<input type="time">
</td>
<td> </td>
</tr>
<tr>
<th>Donovan Cole</th>
<td>
<input type="time">
</td>
<td>
<input type="time">
</td>
<td> </td>
</tr>
<tr>
<th>Robert Hill</th>
<td>
<input type="time">
</td>
<td>
<input type="time">
</td>
<td> </td>
</tr>
<tr>
<th>Douglas Spirs</th>
<td>
$(function() {
function calculate() {
var time1 = $(".Time1").val().split(':'),
time2 = $(".Time2").val().split(':');
var hours1 = parseInt(time1[0], 10),
hours2 = parseInt(time2[0], 10),
mins1 = parseInt(time1[1], 10),
mins2 = parseInt(time2[1], 10);
var hours = hours2 - hours1,
mins = 0;
if (hours < 0) hours = 24 + hours;
if (mins2 >= mins1) {
mins = mins2 - mins1;
} else {
mins = (mins2 + 60) - mins1;
hours--;
}
mins = mins / 60; // take percentage in 60
hours += mins;
hours = hours.toFixed(2);
$(".Hours").val(hours);
}
$(".Time1,.Time2").change(calculate);
calculate();
});
我非常感谢任何帮助,因为我不确定如何让其他员工正确计算时间。
请注意,我不在乎计算是通过一个按钮完成的,还是由于更改&#34;单元格的值而完成的。
答案 0 :(得分:1)
你可以这样做:(这只是一个想法,但它适用于前两行)
https://jsfiddle.net/6ob8q5z9/1/
$(function() {
function calculate() {
$("tr").each(function(i, el) {
if ($(this).find(".Time1").length > 0) {
var time1 = $(this).find(".Time1").val().split(':'),
time2 = $(this).find(".Time2").val().split(':');
var hours1 = parseInt(time1[0], 10),
hours2 = parseInt(time2[0], 10),
mins1 = parseInt(time1[1], 10),
mins2 = parseInt(time2[1], 10);
var hours = hours2 - hours1,
mins = 0;
if (hours < 0) hours = 24 + hours;
if (mins2 >= mins1) {
mins = mins2 - mins1;
} else {
mins = (mins2 + 60) - mins1;
hours--;
}
mins = mins / 60; // take percentage in 60
hours += mins;
hours = hours.toFixed(2);
$(this).find(".Hours").val(hours);
}
});
}
$(".Time1,.Time2").change(calculate);
calculate();
});
答案 1 :(得分:1)
这是一种不同的方法,可以用更少的代码完成您的要求。此外,我继续使用包含所有员工姓名的数组缩短了编写所需的HTML。这使得如果您需要添加或删除员工,则更容易 - 您只需将其删除或直接添加到列表中,并为每个循环创建表行。这使用类似于上述方法的.each(),但使用您用来进行数学计算的时间类型输入。
小提琴:https://jsfiddle.net/arcxdff2/10/
JS
$(function() {
var employees = ['Greg Weiland', 'Alicia Hawly', 'Charlen Connoly', 'Dakota Giles', 'Donovan Cole', 'Robert Hill', 'Douglas Spirs', 'Casey Green', 'Jared Peterson', 'Elizabeth P', 'Carl Mark', 'Carma J.', 'Ike J.', 'Elias H.'];
//This for each loop will write the employee names in the array above into their own rows
for (i = 0; i < employees.length; i++) {
$('#footer').after('<tr class="rowEmployee"><th>' + employees[i] +
'</th><td><input class="Time1" type="time"></td>' +
'<td><input class="Time2" type="time">' +
'</td><td><input class="Hours"></td></tr>')
}
$('#btnSubmit').on('click', function() {
$('.rowEmployee').each(function() {
var time1 = $('.Time1', this).val().split(':');
var time2 = $('.Time2', this).val().split(':');
//The following calculations turn minutes into a fraction to make the math easier
var hoursWorked1 = parseInt(time1[0]) + ((parseInt(time1[1]) == 0) ? 0 : (parseInt(time1[1]) / 60));
var hoursWorked2 = parseInt(time2[0]) + ((parseInt(time2[1]) == 0) ? 0 : (parseInt(time2[1]) / 60));
//Here is your difference in hours calculation below
var diff = hoursWorked2 - hoursWorked1;
//Finally, write the total hours worked to the textbox, rounding to nearest hundredth
$('.Hours', this).val(Math.round(diff*100)/100);
});
});
});
HTML
<table id="actual" cellspacing="0px">
<tr id='header'>
<th>Name</th>
<th>Start Time</th>
<th>End Time</th>
<th>
Total Time
<br/> Worked
</th>
</tr>
<!-- Add the ID of footer so the JS knows where to append the rows containing employee names -->
<tr id="footer">
<td colspan="4"> </td>
</tr>
<tr>
<td colspan="4" align="center">
<button id='btnSubmit'>Submit</button>
</td>
</tr>
</table>