我想要做的是允许用户只在一个字段中输入12小时/ am / pm。
额外:任何方式我可以“从 - 到”验证2个字段,例如:如果用户在字段中输入下午1点,则字段调用将仅允许从下午1点开始转发。
任何帮助都将是真正的帮助。感谢
答案 0 :(得分:1)
$('#time2').attr('disabled', true);
$("#time").timepicker({
timeOnly: true,
timeFormat: 'hh:mm tt'
}).change(function () {
var val = this.value;
if (val !== '') {
var min = val.split(':');
var min1 = min[1].split(' ');
var minMin = parseInt(min1[0]);
var minHour = ((min1[1] == 'am') ? parseInt(min[0]) : parseInt(min[0]) + 12);
$('#time2').removeAttr('disabled').removeClass('hasDatepicker').timepicker({
timeOnly: true,
timeFormat: 'hh:mm tt',
hourMin: minHour,
}).change(function () {
var val_to = this.value;
var min_to = val_to.split(':');
var min1_to = min_to[1].split(' ');
var minMin_to = parseInt(min1_to[0]);
var minHour_to = ((min1_to[1] == 'am') ? parseInt(min_to[0]) : parseInt(min_to[0]) + 12);
if(minHour_to == minHour && minMin_to <= minMin){
this.value = val;
}
});
} else {
$('#time2').attr('disabled', true);
}
});
答案 1 :(得分:1)
您只需要一些代码来解析字符串并找到问题。像这样的逻辑
可能会有一些有用的功能。
"string split".split(" "); // split into an array by spaces
Javascript Equivalent to PHP Explode()
"find the PM".indexOf('PM'); // return turns how many characters
//away the PM is from the left.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
以及slice()
"slice after PM".slice(6); //this will slice from the beginning to the 6th character.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf