我正在尝试在jquery datepicker中创建自定义动态日期额外按钮。但它不起作用......我的代码出错?
$(function () {
$(".datepicker").datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true,
yearRange: "2014:2034",
showButtonPanel: true,
beforeShow: function (input) {
setTimeout(function () {
var buttonPane = $(input)
.datepicker("widget")
.find(".ui-datepicker-buttonpane");
var btn = $('<button class="ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all" type="button">CSA</button>');
btn.unbind("click")
.bind("click", function () {
//$.datepicker._clearDate(input);
//alert('custom text');
$(input).datepicker("hide");
var date = new Date();
date.setMonth(date.getMonth() + 6);
$(input).val(date.getFullYear() + '-'
date.getMonth() + '-' + date.getDate());
});
btn.appendTo(buttonPane);
}, 1);
}
});
});
目标是今天添加一个按钮+ 6个月
答案 0 :(得分:2)
你错过了一个+
改变这个:
$(input).val(date.getFullYear() + '-'
date.getMonth() + '-' + date.getDate());
对此:
$(input).val(date.getFullYear() + '-' +
date.getMonth() + '-' + date.getDate());
另外不要忘记Javascript moth是基于0的,所以如果你想在今天的日期添加6个月,你需要在当前月份添加7,意思是,而不是:
date.setMonth(date.getMonth() + 6);
使用:
date.setMonth(date.getMonth() + 7);
参见示例here