我正在尝试在 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 个月后添加一个按钮
最佳答案
你缺少一个+
更改此:
$(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
关于javascript - datepicker动态日期按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21856022/