问题描述
我目前正在尝试使内联日期选择器对象与日期输入进行交互,并且已经管理了除一件事以外的所有内容.当我尝试使用输入的change事件时,它将引发错误:
I am currently trying to make an inline datepicker object interact with a date input, and have managed everything but one thing. When I try to use the change event of the input, it throws an error:
Uncaught TypeError: $.start_widget.setDate is not a function
我的Django模板/jQuery代码如下,包含在脚本标题中,该脚本标签插入文档的开头:
My Django template/jQuery code is as follows, included in a script tag which is inserted in the head of the document:
$(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 %}
});
然后在将其发送给客户端之前进行处理(仅相关循环):
This is then processed before being sent to the client like this (relevant loop only):
$(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
-对象在change事件处理程序之前已明确定义和初始化,但是它引发了以上异常. console.log
呼叫显示正确的日期.
To me, this looks like the $.start_widget
-object is clearly defined and initialised before the change event handler, and yet it throws the above exception. The console.log
call shows the correct date.
我也尝试用以下方法替换setDate
调用:
I also tried replacing the setDate
call with this:
$.start_widget.datepicker( "setDate", $(this).val() );
但是,我得到了相同的结果.我已经尝试解决了几个小时,但似乎无法自己解决.我的代码有什么问题?
However, I got the same result. I have been trying to figure this out for hours, but seem to be unable to figure it out on my own. What is wrong with my code?
在ImBack的建议下,我收到此错误:
EDIT 1: With ImBack's suggestion, I get this error instead:
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
推荐答案
我自己解决了这个问题.显然,唯一需要做的就是将$(this).val()
转换为new Date($(this).val())
,因为该库无法自行将字符串解析为Date.这使我相信我具有该插件的非标准版本,因为文档明确指出了以下内容:
I figured out the problem on my own. Apparently, the only thing that was necessary was to turn $(this).val()
into new Date($(this).val())
, as the library was unable to parse the string into a Date on its own. This leads me to believe that I have a nonstandard version of the plugin, because of how the docs clearly state:
这篇关于jQuery Datepicker:setDate不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!