是否可以选择在一个字符的数字前添加“0”?
即:1、2、3、4、5、6、7、8、9
变成:01、02、03、04、05、06、07、08、09
是否可以选择在一个字符的数字前添加“0”?
即:1、2、3、4、5、6、7、8、9
变成:01、02、03、04、05、06、07、08、09
工作示例:http: //jsfiddle.net/Gajotres/EHNab/
$(document).ready(function () {
$('#datepicker').datepicker({
beforeShowDay: function (date) {
var dayOfMonth = date.getDate();
if (dayOfMonth >= 1 && dayOfMonth <= 9) {
return [true, 'ui-change-format', ''];
}
return [true, '', ''];
}
});
});
.ui-change-format a::before {
content: "0";
}
我认为,在UI Datepicker
. 您应该为此编写自定义代码:
我写了一些关于beforeShow
和onChangeMonthYear
事件的示例代码,试试这个:
$('#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);
}
});
一些解决方法可能会有所帮助,请参见下文。
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
});