本文介绍了如何防止jQuery引导ClockPicker获得无法被5整除的分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery bootstrap ClockPicker ,并且只需要获取其分钟数可被5整除的时间例如,如果用户选择13:08,则ClockPicker应该选择13:05.

I am using jQuery bootstrap ClockPicker and need to get only times that their minutes are divisible by 5. For example if user selects 13:08, the ClockPicker should select 13:05.

是否有一种方法可以覆盖时钟选择器以舍入选定的分钟数?

Is there a way to override clockpicker to round down selected times minutes?

推荐答案

我没有找到使用 API .所以我用afterDone回调写了一个.

I didn't found a way to do this with the API. So I wrote one using afterDone callback.

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

I parse the input's value and make the change to get the new minutes value.

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;
}
<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

这篇关于如何防止jQuery引导ClockPicker获得无法被5整除的分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 18:29