jquery datepicker默认日期

我试图获取日期选择器的属性来动态提取日期,但是当我尝试将其设置为变量时,我得到未捕获的exception错误。

错误仅发生在没有日历(内联)的页面上。 如何从选择器中拉出rel标签而不会出现此错误?

//Event Calendar Home Page and Listing function calendar_picker () { $("#calendar-inline").datepicker({ //defaultDate: $(this).attr('rel'), dateformat: 'yy-mm-dd', maxDate: '+1y', minDate:'-0d', hideIfNoPrevNext: true, showButtonPanel: false, navigationAsDateFormat: false, onSelect: function(dateText, inst) { var d = new Date(dateText); var fmt1 = $.datepicker.formatDate("yy-mm-dd", d); $.ajax({ type: "POST", url: "/events/listing/all/20/", dataType: "html", date: "event_date="+fmt1, success: function(){ window.location.href= "/events/browse/"+fmt1; }});}}); } 

UPDATE正确,注释行是我遇到的问题,从内部从#calendar-inline中提取属性rel的正确方法是什么。 所有尝试都会在js中抛出未捕获的错误

更新2

 function calendar_picker () { var myDate = new Date($("#calendar-inline").attr('rel')); $("#calendar-inline").datepicker({ dateformat: 'yy-mm-dd', defaultDate:myDate, 

解:

 function calendar_picker () { var myDate = null; if ($("#calendar-inline").attr('rel') != null) { myDate = $.datepicker.parseDate("yy-mm-dd", $("#calendar-inline").attr('rel')); } $("#calendar-inline").datepicker({ dateformat: 'yy-mm-dd', defaultDate:myDate, 

试试这个:

 defaultDate: $("#calendar-inline").attr('rel') 

这将尝试从’rel’属性中提取日期,如果不存在,则应返回null。 当null传递到datepicker默认日期时,它将使用今天作为默认日期。

我们可以这样设置默认日期:

  

请参阅http://jqueryui.com/demos/datepicker/#option-defaultDate

你的问题有点不清楚 – 我假设你的问题出现在注释掉的行上。

如果这是正确的那么我认为你的问题是“这个”。 我不太确定你对“这个”的期望是什么,但在函数内部它将成为窗口对象。

如果我完全走错了轨道,请用更多信息更新问题。

这对我有用:

 var nowDate = new Date(year, month, day, hours, minutes, seconds, milliseconds); 

然后在日期选择器中,您设置:

 defaultDate: nowDate;