设想
一个非常简单的 daterangepicker 实例:
<div class="section">
<label for="drp">date-range-picker</label>
<input type="text" id="drp" />
<script>
(function(){
$('#drp').daterangepicker({
presetRanges: [
{ text: 'Last 30 days',
dateStart: function () { return Date.parse('today').add({day: -30}) },
dateEnd: function () { return Date.parse('today').add({day: -1}); }
},
{ text: 'Previous month'
, dateStart: function () { return Date.today().add({month:-1}).moveToFirstDayOfMonth()}
, dateEnd: function () { return Date.today().add({month:-1}).moveToLastDayOfMonth(); }
},
{ text: 'Current month'
, dateStart: function () { return Date.today().moveToFirstDayOfMonth()}
, dateEnd: function () { return Date.today().add({day:-1}); }
}
],
presets: {dateRange: 'Select range'},
rangeStartTitle: 'Start date', rangeEndTitle: 'End date', doneButtonText: 'Done', prevLinkText: 'Prev', nextLinkText: 'Next',
rangeSplitter: ' - ', dateFormat: "dd/mm/yy", closeOnSelect: true
});
})();
</script>
</div>
其中包括:
<script type="text/javascript" language ="javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" language ="javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script type="text/javascript" language ="javascript" src="client/date.js"></script>
<script type="text/javascript" language ="javascript" src="client/daterangepicker/daterangepicker.jQuery.js"></script>
问题
在本地环境中,一切正常。在生产环境中,选择任何预设后会出现以下错误:
Uncaught TypeError: Cannot call method 'getDate' of null @daterangepicker.jQuery.js:162
引发错误是因为执行下一条语句时:
var range_end = rp.find('.range-end'); [...] var rangeB = fDate( range_end.datepicker('getDate') );
我快疯了。似乎 daterangepicker 没有在.range-end
类元素中初始化 datepicker。但我无法弄清楚出了什么问题。
(Chrome) 控制台中没有其他错误。
参考