简体   繁体   English

将jQuery选择器与Ajax响应一起使用

[英]Using jQuery selectors with Ajax response

I have an Ajax call and response (see the code below.) Prior to this, I had regular javascript code that would successfully turn my select element from a traditional select to a select2 widget. 我有一个Ajax调用和响应(请参见下面的代码。)在此之前,我有常规的javascript代码,可以将我的select元素成功地从传统的select变为select2小部件。

When I try to change the select into the select2 with the javascript code $('#datepicker').datepicker(); 当我尝试使用JavaScript代码$('#datepicker').datepicker();将select更改为select2时$('#datepicker').datepicker(); in the response from Ajax call, I get an error in the debugger that says 在Ajax调用的响应中,我在调试器中收到一条错误消息,内容为

Uncaught TypeError: $(...).select2 is not a function at XMLHttpRequest.xhttp.onreadystatechange (createschedule.php:46)

Here is the code that is inside the javascript function: 这是javascript函数内部的代码:

var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() 
                {
                    if (this.readyState == 4 && this.status == 200) 
                    {
                        document.getElementById("row_"+cur_id).innerHTML = this.responseText;
                        $('#teacherselect').select2({ width: '100%' }); 
                        $('#datepicker').datepicker();
                    }
                };
                xhttp.open("GET", "editrow.php?id="+id, true);
                xhttp.send();

When Ajax returns and this code is executed, does it still have access to the items through jQuery? 当Ajax返回并执行此代码时,它仍然可以通过jQuery访问项目吗? It appears that it does not. 似乎没有。

Because your jQuery and JQuery UI are working correctly, I would start checking the path of the select2. 因为您的jQuery和JQuery UI正常工作,所以我将开始检查select2的路径。 Is it local or a CDN? 是本地还是CDN?

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

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