jQuery 加载后 jquery ui 丢失
我遇到了 .load() 函数后 jQuery 丢失的问题。 我有一个用 jQuery UI 构建的元素。问题是,当我从单独的页面加载它时,如下所示:
$("#mydiv").load("getgroup.php?group=" + selectedGroup).html();
getgroup.php 生成类似这样的内容,具体取决于 GET 参数:
<select>
<option>something</option>
<option>something</option>
</select>
当使用 load() (或 post、ajax、get)加载它时,元素返回未格式化的..我尝试将 jquery 和 jquery-ui 插件也包含在 getgroup.php 文件中,但没有成功...
谢谢
I have a problem with jQuery being lost after .load() function.
I have a element built up with jQuery UI. The problem is that when I load it from separate page like this:
$("#mydiv").load("getgroup.php?group=" + selectedGroup).html();
The getgroup.php generates something like this, depending on the GET parameter:
<select>
<option>something</option>
<option>something</option>
</select>
When loading it with load() (or post, ajax, get) the element returns unformatted... I have tried including jquery and jquery-ui plugin also in the getgroup.php file but with no luck...
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,您的 jquery 代码不会在动态加载的元素上自动执行。
你应该做的是将所有 jquery ui 代码放在一个函数中。例如,假设您要在新加载的提交元素上调用
button()
jQuery UI 函数:然后,在加载新元素时使用此函数作为成功回调 - 给它
target
作为参数以避免在所有 DOM 上重新运行代码。假设您正在 id“container”的div
中加载数据:The problem is, your jquery code isn't executed automatically on dynamically loaded elements.
What you should do is, put all your jquery ui code in a function. For instance, let's say you want to call the
button()
jQuery UI function on the newly loaded submit elements:Then, you use this function as a success callback when you load new elements - giving it
target
as an argument to avoid rerunning the code on all of your DOM. Let's suppose you're loading the data in adiv
of id "container":如果我的理解是正确的并且正在插入元素,但没有 CSS,则可能需要运行 .addClass< /a> 加载元素后,一旦元素存在,就应用您选择的 CSS 类。但是,如果在 CSS 中预定义了元素类型的默认值,例如
select{color:#000000;width:...
这些也应该自动加载。根据下面的评论 - 如果您正在查看预定义处理程序仍然与 AJAX/load() 调用注入的内容相关,您可以使用 .live() 方法:
http://api.jquery.com/live/
If my understanding is correct and the element is being inserted, but with no CSS, you may need to run .addClass after the element is loaded to apply your chosen CSS class once the element is present. However, if in your CSS you have default values for the element type predefined, e.g.
select{color:#000000;width:...
these should also be loaded automatically.Per the comment below- if you are looking at your predefined handlers still being relevant for content injected by AJAX/load() calls, you can use the .live() method:
http://api.jquery.com/live/