首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止jQuery引导ClockPicker获取不可被5整除的分钟

如何防止jQuery引导ClockPicker获取不可被5整除的分钟
EN

Stack Overflow用户
提问于 2016-02-14 12:09:30
回答 2查看 4K关注 0票数 1

我使用的是jQuery引导ClockPicker,只需要得到他们的分钟可以被5整除的时间。例如,如果用户选择13:08,ClockPicker应该选择13:05。

是否有一种方法可以覆盖时钟拾取器,使所选时间缩短几分钟?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-14 12:42:55

我没有找到用API接口做这件事的方法。因此,我使用afterDone回调编写了一个。

我解析输入的值并进行更改以获得新的分钟值。

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://weareoutman.github.io/clockpicker/dist/bootstrap-clockpicker.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://weareoutman.github.io/clockpicker/dist/bootstrap-clockpicker.min.js"></script>

<div class="input-group clockpicker col-xs-6">
  <input type="text" class="form-control" value="09:30">
  <span class="input-group-addon">
    <span class="glyphicon glyphicon-time"></span>
  </span>
</div>

http://jsbin.com/lopufu/edit?html,js

票数 2
EN

Stack Overflow用户

发布于 2016-09-26 16:50:52

如果您不介意对Clockpicker进行更改(我必须为IE做此更改),那么在bootstrap-clockicker.js中添加一个非官方选项,如“圆形调制解调器5”:

  1. 改变这个
代码语言:javascript
复制
// 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;
    }
...
  1. 借此
代码语言:javascript
复制
// 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;
    }
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35391567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档