简体   繁体   English

bootstrap-datepicker在AJAX加载的内容中未定义

[英]bootstrap-datepicker undefined in AJAX loaded content

I am using bootstrap-datepicker from this repo: https://github.com/eternicode/bootstrap-datepicker homepage: http://eternicode.github.io/bootstrap-datepicker 我正在此仓库中使用bootstrap-datepicker: https : //github.com/eternicode/bootstrap-datepicker主页: http : //eternicode.github.io/bootstrap-datepicker

It works just fine on a form with datefields. 它在带有日期字段的表单上很好用。 However, on that form I also have a button which opens a subform in a modal and that subform also has datefields. 但是,在该表单上,我还具有一个按钮,该按钮可打开模态下的子表单,并且该子表单也具有日期字段。 The problem is that on that subform, the datepicker is undefined and hence does not work. 问题在于该子窗体上的日期选择器未定义,因此无法正常工作。

In the modal form page loaded using AJAX, I have this snippet: 在使用AJAX加载的模式表单页面中,我有以下代码段:

<script type="text/javascript">
$(document).ajaxComplete(function()
{
    var thing = $('.date-container input');
    console.log(thing);        //prints as a valid element
    console.log($.datepicker); //prints as 'undefined'
    thing.datepicker({         //crashes the script due to .datepicker being undefined
        autoclose: true,
        todayHighlight: true
    });
});
</script>

I even tried running this code from the documentation to set a format in the modal script but that snipped also caused an error saying that datepicker is undefined: 我什至尝试从文档中运行此代码来设置模式脚本中的格式,但该代码片段也引起错误,提示未定义datepicker:

$.fn.datepicker.defaults.format = "mm/dd/yyyy";

So it seems like for some reason, in the script contained in the modal loaded through AJAX, datepicker is simply undefined, but in the parent page on which the modal is loaded, the datepicker works fine. 因此,似乎出于某种原因,在通过AJAX加载的模式中包含的脚本中,datepicker只是未定义,但是在加载模式的父页面中,datepicker可以正常工作。

Any ideas on what the problem might be? 关于可能是什么问题的任何想法?

Are you loading jQuery again in the modal page? 您是否要再次在模式页面中加载jQuery? I suspect this may be the culprit. 我怀疑这可能是罪魁祸首。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM