将事件处理程序动态绑定到jQuery中已过滤元素的父级

时间:2021-04-09 20:27:29

I'm developing a web-app in Rails using jQuery where a lot of the processing is via AJAX POSTs of forms. I need to set up handlers for the various "ajax:" events (ajax:success, ajax:failure), which I think trigger on the <form> element.

我正在使用jQuery在Rails中开发一个web应用程序,其中许多处理是通过表单的AJAX POST。我需要为各种“ajax:”事件(ajax:success,ajax:failure)设置处理程序,我认为它会触发

元素。

Due to Rails, it's easy for me to add a class to the <input type="submit"> elements, but not always easy to add a class to the surrounding form. Finally, because I'm using AJAX to update the page, I obviously want to bind the handlers to any newly-added forms that need them.

由于Rails,我很容易在元素中添加一个类,但并不总是很容易将类添加到周围的表单中。最后,因为我正在使用AJAX来更新页面,所以我显然希望将处理程序绑定到任何需要它们的新添加表单。

Is it possible, using live() or similar, to dynamically bind an event to the <form> tag which is the parent of each <input> tag with a given class that exists now or in the future?

是否可以使用live()或类似方法动态地将事件绑定到

标记,该标记是每个 标记的父级,具有现在或将来存在的给定类?

2 个解决方案

#1


1  

The best way to do this would be to use jQuery's global Ajax event handlers,

执行此操作的最佳方法是使用jQuery的全局Ajax事件处理程序,

#2


0  

sure. Use the parent() or parents() to choose it.

当然。使用parent()或parents()来选择它。

e.g.

例如

$("input:submit").each(function(){
    $(this).parents("form").live(...);
});

#1


1  

The best way to do this would be to use jQuery's global Ajax event handlers,

执行此操作的最佳方法是使用jQuery的全局Ajax事件处理程序,

#2


0  

sure. Use the parent() or parents() to choose it.

当然。使用parent()或parents()来选择它。

e.g.

例如

$("input:submit").each(function(){
    $(this).parents("form").live(...);
});