0

是否可以选择在一个字符的数字前添加“0”?

即:1、2、3、4、5、6、7、8、9

变成:01、02、03、04、05、06、07、08、09

在此处输入图像描述

4

3 回答 3

3

工作示例:http: //jsfiddle.net/Gajotres/EHNab/

JavaScript:

$(document).ready(function () {
    $('#datepicker').datepicker({
        beforeShowDay: function (date) {
            var dayOfMonth = date.getDate();
            if (dayOfMonth >= 1 && dayOfMonth <= 9) {
                return [true, 'ui-change-format', ''];
            }
            return [true, '', ''];
        }
    });
});

CSS:

.ui-change-format a::before { 
  content: "0";
}
于 2014-06-10T11:06:39.913 回答
1

我认为,在UI Datepicker. 您应该为此编写自定义代码:

我写了一些关于beforeShowonChangeMonthYear事件的示例代码,试试这个:

$('#picker').datepicker({
    beforeShow: function (txt, inst) {
      setTimeout(function(){
         $("#ui-datepicker-div").find('table tr td[data-handler="selectDay"] > a').each(function(){
             if($(this).text().trim().length < 2){
               $(this).text("0" + $(this).text())
             }
         });            
      }, 100);
    },
    onChangeMonthYear: function (year, month, inst) {
      setTimeout(function(){
         $("#ui-datepicker-div").find('table tr td[data-handler="selectDay"] > a').each(function(){
             if($(this).text().trim().length < 2){
                $(this).text("0" + $(this).text())
             }
         });            
      }, 100);
    }
});

工作示例

于 2014-06-10T11:27:19.680 回答
-1

一些解决方法可能会有所帮助,请参见下文。

function addZero() {
    setTimeout(function () {
        $('.ui-state-default').each(function () {
            var t = $(this).text();
            if (t.length == 1) {
                $(this).text('0' + t);
            }
        });
    }, 10);
}
$('#datepicker').datepicker({
    onChangeMonthYear: addZero,
    beforeShow: addZero
});
于 2014-06-10T11:18:09.730 回答