確定這裏的主要區別:(代碼可以使用的更優化當然,不過這工作)
var duration = '01 Hrs 30 Mins 20 Secs';
$(document).ready(function(){
$('#total-duration').html(duration);
$(function() {
$('#questiondurationpicker').trenttimepicker({
timeFormat:'hh mm ss',
hourGrid: 4,
minuteGrid: 10,
secondGrid: 10,
showOn: 'button',
buttonImage: "Images/clock.gif",
buttonImageOnly: true
});
});
$(".questiondurationpickerRow").live("change", calculateDuration);
$("#addQuestionBtn").live("click", calculateDuration);
});
function insertQuestion(form) {
var $tr = $("<tr></tr>");
var $duration = $("<td class='duration'></td>");
$('#questiondurationpicker').each(function() {
var $this = $(this);
var $durationText = $("<input type='text' class='questiondurationpickerRow' readonly='readonly' />").attr('name',$this.attr('name')).attr('value',$this.val())
$duration.append($durationText);
});
$tr.append($duration);
$('#qandatbl').append($tr);
$('.questiondurationpickerRow').trenttimepicker({
timeFormat:'hh mm ss',
hourGrid: 4,
minuteGrid: 10,
secondGrid: 10,
showOn: 'button',
buttonImage: "Images/clock.gif",
buttonImageOnly: true
});
}
var format = duration.match(/(\d\d)/ig),
hours = parseInt(format[0], 10),
mins = parseInt(format[1], 10),
secs = parseInt(format[2], 10);
function calculateDuration()
{
var totalduration = duration;
var sign = '';
var tmp_hours = 0;
var tmp_mins = 0;
var tmp_secs = 0;
$("#qandatbl td.duration input").each(function(){
tmp_format = $(this).val().match(/(\d\d)/ig),
tmp_hours += parseInt(tmp_format[0], 10),
tmp_mins += parseInt(tmp_format[1], 10),
tmp_secs += parseInt(tmp_format[2], 10);
});
newH = hours - tmp_hours;
newM = mins - tmp_mins;
newS = secs - tmp_secs;
if(newS < 0) {
newS += 60;
newM--;
}
if(newM < 0) {
newM += 60;
newH--;
}
if(newH < 0) {
newH = tmp_hours - hours;
newM = tmp_mins - mins;
newS = tmp_secs - secs;
if(newS < 0) {
newS += 60;
newM--;
}
if(newM < 0) {
newM += 60;
newH--;
}
sign = '- ';
}
checkedH = (newH < 10 ? '0' : '') + newH;
checkedM = (newM < 10 ? '0' : '') + newM;
checkedS = (newS < 10 ? '0' : '') + newS;
new_duration = sign + checkedH + ' Hrs ' + checkedM + ' Mins ' + checkedS + ' Secs';
$("#total-duration").text(new_duration);
}
請與timepicker添加一個工作代碼和所有使我們可以用它測試 – redmoon7777 2012-01-08 06:32:16
這裏試試這個鏈接http://helios.hud.ac.uk/u0867587/testing/Testpage .php,它應該工作,唯一不同的是var的持續時間不是var duration =「01小時30分20秒」;而是var duration ='<?php echo「0 1小時30分鐘20秒「?>';由於持續時間將從php變量 – BruceyBandit 2012-01-08 06:37:38
中檢索,所以問題是總持續時間不會改變,對吧? – redmoon7777 2012-01-08 07:10:19