.live()、.delegate() 或 .on() 都不适用于动态加载到页面的表单

发布于 2024-12-28 08:56:52 字数 744 浏览 3 评论 0原文

我有一个无限滚动的页面。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满栀 2025-01-04 08:56:52

您在这里使用了错误的事件委托。

表单动态加载,因此当代码运行时它们不包含在$("form:not(.member)")中,它们还不存在。

在包含表单的父元素上使用委托

$('#formsContainer').on('submit', 'form:not(.member)', function() {
     ...
});

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:

$('#formsContainer').on('submit', 'form:not(.member)', function() {
     ...
});
亚希 2025-01-04 08:56:52

你的语法只是错误的,仅此而已。这些功能应该可以工作。以 .on() 为例:

$('#someListener').on('submit', 'form:not(.member)', function() { ... });

其中 someListener 是永远不会被 AJAX 函数破坏的祖先元素。如果您尝试绑定到被破坏的元素,您的侦听器也会丢失。

必须承认,我认为 'form:not(".member")' 部分对我来说也不合适。您确定这是 not 的正确语法吗?我只是相信你的话。

无论如何,有一个更好的方法:要么 #someListener 是一个祖先,其中没有其他表单(如搜索表单),要么您为这种特定类型的表单提供一个类。因此,您不必排除 .member 表单,而是只针对这种表单:

$('#someListener').on('submit', '.dynamicForm', function() { ... });

或者,如果 Ajax 内容块(表单和所有)具有某种包装器,您可以向其中添加一个类(例如,".ajaxBlock")并按以下方式执行:

$('#someLIstener').on('submit', '.ajaxBlock form', function() { ... });

Your syntax is just wrong, that's all. Those functions should work. Using .on() as an example:

$('#someListener').on('submit', 'form:not(.member)', function() { ... });

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 for not? 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:

$('#someListener').on('submit', '.dynamicForm', function() { ... });

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:

$('#someLIstener').on('submit', '.ajaxBlock form', function() { ... });
温柔女人霸气范 2025-01-04 08:56:52

上述答案均无效。解决方案是将 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.

熟人话多 2025-01-04 08:56:52
$("#parentContainer").on("submit", "form:not(.member)", function() { // code });
$("#parentContainer").on("submit", "form:not(.member)", function() { // code });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文