【发布时间】:2015-12-14 05:46:42
【问题描述】:
我正在使用 Jquery UI datepicker 来允许用户通过从显示的日历中选择日期来填写日期输入。
到目前为止,一切正常:http://jsfiddle.net/Aut9b/374/
然后,我想突出显示某些日期,以帮助用户选择,所以我查看了 beforeShowDay 选项,这使得这成为可能。
beforeShowDayType: 函数(日期日期)
Default: null A function that takes a date as a parameter and must return an array with: [0]: true/false indicating whether or not this date is selectable [1]: a CSS class name to add to the date's cell or "" for the default presentation [2]: an optional popup tooltip for this date The function is called for each day in the datepicker before it is displayed.
这里是演示:http://jsfiddle.net/Aut9b/375/
下一步不仅要突出显示某些日期,还要根据用户之前在其他输入中选择的内容(以相同的形式)动态地进行,所以我使用ajax 来检索日期突出显示
这是我目前的(不完整的)代码。
$(function() {
$('.datepicker').datepicker({
dateFormat : 'yy-mm-dd'
});
});
function fillDates() {
$('.datepicker').datepicker({
beforeShowDay: function( date ) {
var highlight = dates[date];
if( highlight ) {
return [true, 'highlight', highlight];
} else {
return [true, '', ''];
}
}
});
}
function getDates() {
$.ajax({
type : "POST",
dataType: "text",
url : "ajaxFindDates",
data : {departure:$('#departure option:selected').val(),
arrival:$('#arrival option:selected').val()},
success : function(data) {
var dateStr = JSON.parse(data);
var dates=[];
for (var i = 0; i < dateStr.length; i++) {
date=new Date(dateStr[i]);
dates.push(date);
}
fillDates(dates);
}
,
error : function(data) {
alert("Problem!" );
}
});
}
当<select>的值发生变化时,函数getDates()被调用。
我尝试使用浏览器开发工具调试,似乎beforeShowDay中定义的函数从未执行过。
任何帮助将不胜感激! 谢谢。
【问题讨论】:
-
你的ajax执行成功了吗?
-
另外,使用`dataType: "text",`然后
var dateStr = JSON.parse(data);是多余的。你可以简单地使用dataType: "json"。 -
是的。这是返回的数据(例如):"["2015-09-12","2015-09-11"]"
-
能否在 beforeShowDay 函数中插入“console.log( dates[0], date )”代码并写入结果。
-
我刚试了没结果,好像没有执行 beforeShowDay 函数Link
标签: javascript jquery ajax jquery-ui datepicker