本文介绍了jQuery的.on()方法与Submit事件相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .on()上遇到了问题.我有多个表单元素(具有 class ="remember" 的表单),我也使用AJAX添加了另一个 form.remember .因此,我希望它处理类似以下事件的提交事件:

I've got a problem with .on().I have multiple form-elements (forms with class="remember"), also I add another one form.remember using AJAX.So, I want it to handle submit event something like:

$('form.remember').on('submit',function(){...})

但是添加了AJAX的表单无法使用.

but form added with AJAX doesn't work with it.

问题出在哪里?是虫子吗?

Where is the problem? Is it a bug?

推荐答案

您需要将事件委托给文档级别

You need to delegate event to the document level

$(document).on('submit','form.remember',function(){
   // code
});

$('form.remember').on('submit'的工作方式与 $('form.remember').submit()相同,但是当您使用 $(document).on('submit','form.remember'然后它也适用于以后添加的DOM.

$('form.remember').on('submit' work same as $('form.remember').submit( but when you use $(document).on('submit','form.remember' then it will also work for the DOM added later.

这篇关于jQuery的.on()方法与Submit事件相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:42