.live()、.delegate() 或 .on() 都不适用于动态加载到页面的表单
我有一个无限滚动的页面。 ajax 功能对于最初加载时页面上存在的表单可以正常工作,但对于从无限滚动 ajax 功能加载的表单不起作用。我已经搜索了大约 5 天试图找到答案,但还没有。以下是 javascript 的一部分:
$("form:not(.member)").submit(function() {
var status_id = $('#status_id').val(),
etc..........
:not(.member)
部分的原因是页面上的其他表单(例如搜索表单)不包含在脚本中。
我已经尝试过
$("form:not(.member)").live('submit', function() {
,
$("form:not(.member)").on('submit', function() {
并且
$('body').delegate('submit', 'form:not(.member)', function() {
这些都不适用于初始页面加载后加载的表单。 有人还有其他建议吗? 顺便说一句,我正在使用jquery 1.7.1。 我也尝试过
$("form:not(.member)").bind('submit', function()
I have a page with infinite scroll. The ajax functionality works fine for forms existing on the page when it initially loads, but doesn't work for forms loaded from the inifinite scroll ajax feature. I've searched for about 5 days trying to find a answer and haven't yet. Here is a portion of the javascript:
$("form:not(.member)").submit(function() {
var status_id = $('#status_id').val(),
etc..........
The reason for the :not(.member)
part is so the other forms on the page, such as the search form, aren't included in the script.
I've tried
$("form:not(.member)").live('submit', function() {
and
$("form:not(.member)").on('submit', function() {
as well as
$('body').delegate('submit', 'form:not(.member)', function() {
None of these are working on the forms loaded AFTER the initial page load.
Does anyone have any other suggestions?
I'm using jquery 1.7.1 by the way.
I've also tried
$("form:not(.member)").bind('submit', function()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在这里使用了错误的事件委托。
表单也动态加载,因此当代码运行时它们不包含在
$("form:not(.member)")
中,它们还不存在。在包含表单的父元素上使用委托:
You are using the event delegation wrong here.
The forms are also loaded dynamically, so they are not included in
$("form:not(.member)")
when the code runs, they don't exist yet.Use delegation on a parent element that contains the forms:
你的语法只是错误的,仅此而已。这些功能应该可以工作。以
.on()
为例:其中
someListener
是永远不会被 AJAX 函数破坏的祖先元素。如果您尝试绑定到被破坏的元素,您的侦听器也会丢失。必须承认,我认为
'form:not(".member")'
部分对我来说也不合适。您确定这是not
的正确语法吗?我只是相信你的话。无论如何,有一个更好的方法:要么 #someListener 是一个祖先,其中没有其他表单(如搜索表单),要么您为这种特定类型的表单提供一个类。因此,您不必排除
.member
表单,而是只针对这种表单:或者,如果 Ajax 内容块(表单和所有)具有某种包装器,您可以向其中添加一个类(例如,
".ajaxBlock"
)并按以下方式执行:Your syntax is just wrong, that's all. Those functions should work. Using
.on()
as an example:Where
someListener
is an ancestor element that is NEVER destroyed by the AJAX function. If you're trying to bind to an element that gets destroyed, your listener gets lost as well.Have to admit, I don't think the
'form:not(".member")'
part doesn't look right to me, either. Are you sure that's the correct syntax fornot
? I'm just taking your word for it here.There's a better way anyhow: either #someListener is an ancestor that does not have your other forms within (like the search form), or you give this specific kind of form a class. So instead of excluding
.member
forms, you would TARGET just this kind of form:Or, if the Ajax content block (form and all) has a wrapper of some sort, you could add a class to it (say,
".ajaxBlock"
) and do it this way instead:上述答案均无效。解决方案是将 onclick="function" 分配给所有评论表单,包括已加载的评论表单。
None of the above answers worked. The solution was assigning onclick="function" to all of the commenting forms including the ones already loaded.