如何防止jQuery引导ClockPicker获得不能被5整除的分钟
我正在使用jQuery引导程序ClockPicker,并且只需要获得它们的分钟可被5整除的次数。例如,如果用户选择13:08,则ClockPicker应选择13:05。
有没有办法覆盖clockpicker来舍入选定的时间分钟?
我没有找到使用API执行此操作的方法。 所以我用afterDone
回调写了一个。
我解析输入的值并进行更改以获得新的分钟值。
var clockpicker = $('.clockpicker').clockpicker({ afterDone: function() { clockpicker.val(round()); } }).find('input'); function round() { var time = clockpicker.val(), arr = time.split(':'), hour = arr[0], min = arr[1], newMin = (Math.floor(parseInt(min) / 5)) * 5; return hour + ':' + (newMin > 9 ? '' : '0') + newMin; }
如果您不介意对Clockpicker进行更改(我必须为IE执行此操作),请在bootstrap-clockpicker.js中添加非官方选项,如“roundmodfive”:
改变这一点
// Hours and minutes are selected ClockPicker.prototype.done = function() { raiseCallback(this.options.beforeDone); this.hide(); var last = this.input.prop('value'), value = leadingZero(this.hours) + ':' + leadingZero(this.minutes); if (this.options.twelvehour) { value = value + this.amOrPm; } ...
由此而来
// Hours and minutes are selected ClockPicker.prototype.done = function() { raiseCallback(this.options.beforeDone); this.hide(); var last = this.input.prop('value'); //Unofficial option added /* Just an explain: I like rounding. Thus, for example 43min => 45 and 42min => 40. The minutes are base 60, so you have to make modulus 60. */ if (this.options.roundmodfive) { this.minutes = (Math.round(parseInt(this.minutes) / 5)) * 5 % 60; } var value = leadingZero(this.hours) + ':' + leadingZero(this.minutes); if (this.options.twelvehour) { value = value + this.amOrPm; } ...