【发布时间】:2016-11-01 10:36:48
【问题描述】:
我目前正在尝试使内联日期选择器对象与日期输入交互,并且除了一件事之外已经管理了所有内容。当我尝试使用输入的更改事件时,它会抛出一个错误:
Uncaught TypeError: $.start_widget.setDate is not a function
我的 Django 模板/jQuery 代码如下,包含在插入文档头部的脚本标签中:
$(document).ready(function(){
{% for string in datepickerstrings %}
jQuery.{{ string }}_widget = $('#datepicker-{{ string }}');
$.{{ string }}_widget.datepicker({
inline: true,
altField: '#event-{{ string }}',
onSelect: function (date, instance) {
$('#event-{{ string }}').val(date);
}
});
$.{{ string }}_widget.hide();
$('#event-{{ string }}').focusin(function () {
$.{{ string }}_widget.show();
});
$('#{{ string }}-close').on("click", function (e) {
e.preventDefault();
$.{{ string }}_widget.hide();
});
$('#event-{{ string }}').change(function () {
console.log("Changed date value from field: " + $(this).val());
$.{{ string }}_widget.setDate($(this).val());
});
{% endfor %}
});
然后在像这样发送到客户端之前对其进行处理(仅相关循环):
$(document).ready(function(){
jQuery.start_widget = $('#datepicker-start');
$.start_widget.datepicker({
inline: true,
altField: '#event-start',
onSelect: function (date, instance) {
$('#event-start').val(date);
}
});
$.start_widget.hide();
$('#event-start').focusin(function () {
$.start_widget.show();
});
$('#start-close').on("click", function (e) {
e.preventDefault();
$.start_widget.hide();
});
$('#event-start').change(function () {
console.log("Changed date value from field: " + $(this).val());
$.start_widget.setDate($(this).val());
});
});
在我看来,$.start_widget-object 在更改事件处理程序之前已明确定义和初始化,但它会引发上述异常。 console.log 调用显示正确的日期。
我还尝试将 setDate 调用替换为:
$.start_widget.datepicker( "setDate", $(this).val() );
但是,我得到了相同的结果。我一直试图解决这个问题几个小时,但似乎无法自己解决。我的代码有什么问题?
编辑 1:根据 ImBack 的建议,我得到了这个错误:
Uncaught TypeError: date.getDate is not a function
_setDate @ ui.datepicker.js:1077
_setDateDatepicker @ ui.datepicker.js:243
(anonymous function) @ ui.datepicker.js:1426
each @ jquery-1.8.3.min.js:2
each @ jquery-1.8.3.min.js:2
$.fn.datepicker @ ui.datepicker.js:1424
(anonymous function) @ (index):66
dispatch @ jquery-1.8.3.min.js:2
u @ jquery-1.8.3.min.js:2
【问题讨论】:
-
使用这个
jQuery.start_widget.datepicker( "setDate", $(this).val());它会为你工作 -
@I'mBack 遗憾的是,它没有。我收到了相同类型的错误,但描述略有不同。我将它包含在问题的编辑中。
标签: jquery datepicker django-templates jquery-ui-datepicker typeerror