[英]AJAX loaded JQuery datepicker only working after alert
I got a pretty odd question. 我有一个很奇怪的问题。 I created a field with a datepicker assigned. 我创建了一个分配了日期选择器的字段。
This field is being loaded by an AJAX call into a div. 该字段由AJAX调用加载到div中。
In summarry this is working already.. But there's a catch. 总而言之,这已经起作用了。但是有一个陷阱。 It only works if I set an alert code before I initiate the datepicker. 仅当我在启动日期选择器之前设置了警报代码时,它才起作用。
When I remove the alert. 当我删除警报时。 It simply stops working. 它只是停止工作。
The init code is within another function: 初始化代码在另一个函数中:
alert('kut');
$("#mini_calendar").datepicker({
defaultDate: "now",
constrainInput: false,
changeMonth: true,
numberOfMonths: 2,
onClose: function (selectedDate) {
}
});
I hope someone knows what I am doing wrong. 我希望有人知道我在做什么错。
EDIT 编辑
Thanks a million. 太感谢了。
The solution is : 解决方案是:
interval = setInterval(function(){
if($("#mini_calendar").length > 0) {
$("#mini_calendar").datepicker({
defaultDate: "now",
constrainInput: false,
changeMonth: true,
numberOfMonths: 2,
onClose: function (selectedDate) {
}
});
clearInterval( interval );
}
}, 300);
Edit: 编辑:
I am calling these on Ajax success : 我称这些是Ajax的成功:
///######## IN CASE OF SUCCESS
success: function (response) {
if (response != '') {
$("[@targetDiv]").html(response);
DateNav_CalendarInit(DisplayType);
}
else {
alert('error! Something went wrong during the obtaining of data!');
}
}
The "DateNav_CalendarInit()" calls the initialisation code “ DateNav_CalendarInit()”调用初始化代码
Without the alert you are trying to capture DOM element before it actually exists. 在没有警报的情况下,您试图在DOM元素实际存在之前对其进行捕获。
You should wait for the Document Ready event: 您应该等待文档就绪事件:
Example: 例:
$(function(){
// .. your code here ..
}
...or simply put your javascript code at the bottom of the tag (or at least after *#mini_calendar" element). ...或只是将您的JavaScript代码放在标记的底部(或至少在*#mini_calendar“元素之后)。
I also faced the same problem few days ago i did this by jquery load() function. 我几天前也通过jquery load()函数做到了这一点。
$('body').on('load change','your container element in which #mini_calendar will load',function(){ $("#mini_calendar").datepicker({ defaultDate: "now", constrainInput: false, changeMonth: true, numberOfMonths: 2, onClose: function (selectedDate) { } }); });
don't forget to replace container element selector. 不要忘记替换容器元素选择器。 otherwise code will not work 否则代码将无法正常工作
if not this than you can set interval that will check if the $('mini_calendar') exists or not in an element. 如果不是,则可以设置间隔,该间隔将检查$('mini_calendar')是否存在于元素中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.