В основном у меня есть четыре таймера, один для startTime и один для endTime. Время начала делится на startHour и startMin, и то же самое относится к endTime. Вот как я настроил startTime:
htmlStr += "<tr><td><input type='button' id='startHourPlus' value='+' class='qtyplus' field='quantity' onClick='plusHour();' /><input type='button' id='startMinPlus' value='+' class='qtyplus' field='quantity' onClick='plusMin();' /><br/>";
htmlStr += "<input type='text' id='startHour' name='quantity' value='0' class='qty' /><input type='text' id='startMin' name='quantity' value='0' class='qty' /><br/>";
htmlStr += "<input type='button' id='startHourMinus' value='-' class='qtyminus' field='quantity' onClick='minusHour();'/><input type='button' id='startMinMinus' value='-' class='qtyminus' field='quantity' onClick='minusMin();' /></td>";
И для endTime:
htmlStr += "<td><input type='button' id='endHourPlus' value='+' class='qtyplus' field='quantity' onClick='plusHour();' /><input type='button' id='endMinPlus' value='+' class='qtyplus' field='quantity' onClick='plusMin();' /><br/>";
htmlStr += "<input type='text' id='endHour' name='quantity' value='0' class='qty' /><input type='text' id='endMin' name='quantity' value='0' class='qty' /><br/>";
htmlStr += "<input type='button' id='endHourMinus' value='-' class='qtyminus' field='quantity' onClick='minusHour();' /><input type='button' id='endMinMinus' value='-' class='qtyminus' field='quantity' onClick='minusMin();' /></td></tr>";
Когда нажимаются плюс и минус, они выполняют ту же функцию. Вот мой JavaScript:
function plusHour() {
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name=' + fieldName + ']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
if (currentVal < 24) {
$('input[name=' + fieldName + ']').val(currentVal + 1);
}
}
// Otherwise put a 0 there
$('input[name=' + fieldName + ']').val(0);
}
function plusMin() {
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name=' + fieldName + ']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
if (currentVal < 60) {
$('input[name=' + fieldName + ']').val(currentVal + 1);
}
}
// Otherwise put a 0 there
$('input[name=' + fieldName + ']').val(0);
}
function minusHour() {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name=' + fieldName + ']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$('input[name=' + fieldName + ']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name=' + fieldName + ']').val(0);
}
}
function minusMin() {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name=' + fieldName + ']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$('input[name=' + fieldName + ']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name=' + fieldName + ']').val(0);
}
}
Вот моя скрипка: Fiddle Первая и вторая - для startTime, последние две - для endTime.
Однако, когда я нажимаю кнопку увеличения и уменьшения во время начала или окончания, текстовое поле не показывает значение. Интересно, какую часть я сделал неправильно.
Заранее спасибо.
$(this).val()
вместоvar currentVal = parseInt($('input[name=' + fieldName + ']').val());
? - person T J   schedule 18.06.2014parseInt
со своим вторым параметром, чтобы убедиться, что вы не получаете взамен неожиданные значения.parseInt(myVal,10)
для десятичных чисел. - person christian314159   schedule 18.06.2014