0
如果此問題已被詢問,請讓我知道在哪裏可以找到答案。使用jQuery計算兩個表格之間的差異
我正在設置一個包含三個不同表格的「電子表格」樣式表單。第一張表格將包含僱員名單及其開始時間/結束時間以及總工作時間。第二個表格將包含員工計劃的開始時間/結束時間/總時間。第三張表格應包含實際工作時間與計劃工作時數之間的差異。
我已經能夠獲得工作/計劃工作總時間的計算,但獲取jquery公式來計算差異是行不通的。
這裏是包含該頁面的的jsfiddle:https://jsfiddle.net/Dragoonman/6oyauwr8/
jQuery的
// JavaScript Document
$(document).ready(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').before('<tr class="rowEmployee"><th>' + employees[i] +
'</th><td><input class="Time1" type="text"></td>' +
'<td><input class="Time2" type="text">' +
'</td><td><input class="Hours"></td></tr>');
$('#footer2').before('<tr class="rowSchedule"><td><input class="Time3" type="text"></td>' +
'<td><input class="Time4" type="text"></td>' +
'<td><input class="Hours2"></td></tr>');
$('#footer3').before('<tr class="rowDiff"><td><input class="diff"></td></tr>');
}
$('.Time2').on('change', 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);
});
});
$('.Time4').on('change', function() {
$('.rowSchedule').each(function() {
var time1 = $('.Time3', this).val().split(':');
var time2 = $('.Time4', 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
$('.Hours2', this).val(Math.round(diff * 100)/100);
});
});
$('.Time2').on('change', function() {
$('.rowDiff').each(function() {
var time1 = $('#footer.rowEmployee.Hours', this).val();
var time2 = $('#footer2.rowSchedule.Hours2', this).val();
var diff = time2 - time1;
$('.diff', this).val(diff);
});
});
});
HTML
<h3>IMPUT ALL TIMES IN MILITARY FORMAT INCLUDING ":"</h3>
<h4>*times after midnight for same working day should continue to 25:00+*</h4>
<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>
</table>
<table id="schedule" cellspacing="0px">
<tr>
<th>
Scheduled
<br/> Start Time
</th>
<th>
Scheduled
<br/> End Time
</th>
<th>
Total Time
<br/> Scheduled
</th>
</tr>
<tr id="footer2">
<td colspan="4"> </td>
</tr>
</table>
<table id="difference" cellspacing="0px">
<tr>
<th>
Difference
<br/>
</th>
</tr>
<tr id="footer3">
<td colspan="4"> </td>
</tr>
</table>
可以提供將不勝任何幫助表示讚賞
謝謝你的建議。原始的jquery代碼被設計用於一個可以同時計算所有行的按鈕。我對它進行了修改,以便在每行都被填充的情況下,它會針對該行進行計算。另外,在這兩個函數中添加的if()語句提供了我正在尋找的功能,所以再次感謝您。 – Dragonman86