当通过ajax加载表单时,jQuery的live和livequery对我不起作用
我之前问过类似的问题 如何在插入元素后让 jquery 将行为附加到元素,但仍然没有找到答案。为了让事情变得清楚,我用一个不同的例子提出一个新问题。
这是我的代码,不起作用。
$(document).ready(function() {
$('form.edit_color').live('submit', function(){
var form = $(this).closest('form');
var colorRow = $(this).closest('tr');
var action = $(form).attr('action');
var formData = $(form).serialize();
$(colorRow).fadeOut();
$.post(action, formData,
function(data) {
$(form).replaceWith(data);
$(colorRow).fadeIn();
});
return false;
});
});
发生的情况是这样的,对于我的每个表单,我都可以通过 ajax 提交它们并将其替换为更新的表单。但是,当我单击新的提交按钮时,什么也没有发生。
I asked a similar question earlier How can I have jquery attach behavior to an element after inserting it, but still have not found an answer. To keep things clear I am asking a new question with a different example.
Here is my code that doesn't work.
$(document).ready(function() {
$('form.edit_color').live('submit', function(){
var form = $(this).closest('form');
var colorRow = $(this).closest('tr');
var action = $(form).attr('action');
var formData = $(form).serialize();
$(colorRow).fadeOut();
$.post(action, formData,
function(data) {
$(form).replaceWith(data);
$(colorRow).fadeIn();
});
return false;
});
});
What happens is this, for each of my forms I can submit them through ajax and replace it with the updated form. But, then nothing happens when I click the new submit button.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要添加到 bobince,
live
使用事件委托 “附加”事件的技术。它的工作原理是所有事件最终都会传播到 DOM 中的最高父级 (window.document
),因此如果您仅将一个事件处理程序附加到文档,然后根据原始目标运行不同的事件处理程序事件的。然而,有些事件不会传播。
submit
就是其中之一。所以你不能在那里使用事件委托。To add to bobince,
live
uses event delegation technique to 'attach' events. It works on a basis that all events eventually propagate to the highest parent in DOM (window.document
) so if you attach just a single event handler to document and then run different event handlers based on the original target of the event.Some events however, do not propagate.
submit
is one of them. So you can't use event delegation there.正如您在原始问题中所回答的,
live
根本不适用于submit
事件。另请注意,回调函数中的
$(colorRow).fadeIn();
不可能起作用,因为它引用submit
发生。但您已在前面的replaceWith
调用中完全删除了该 colorRow 并将其替换为新的 colorRow。替换后,form
和colorRow
变量不会指向任何有用的内容。live
是一种 hack,与事件在 HTML 中的实际工作方式不太相符。尽量避免使用它。用新标记替换页面的大部分内容通常是一个糟糕的举动。如果您可以从 JSON 返回值更新表单中现有元素的内容,而不是传回 HTML 的 katamari:那么您将无需担心重新绑定事件或保留对现已失效的 DOM 对象的引用。(如果一定要传回HTML,至少只返回表单的内容,而不是表单标签本身。那么你可以使用
html()
来设置表单内容;通过不替换表单标签本身,您不必担心重新绑定submit
事件。)As answered in your original question,
live
simply does not work with thesubmit
event.Note also the
$(colorRow).fadeIn();
in your callback function can't possibly work as it is referring to the colorRow that existed in the form at the timesubmit
occurred. But you have completely removed and replaced that colorRow with a new one in the precedingreplaceWith
call. After the replace, theform
andcolorRow
variables don't point to anything useful.live
is a bit of a hack that doesn't quite mesh with how events actually work in HTML. Try to avoid having to use it. Replacing large parts of your page with new markup is often a bad move. If you can update the content of existing elements in the form from a JSON return value rather than passing back a katamari of HTML: then you won't need to worry about rebinding events or keeping references to now-dead DOM objects.(If you must pass back HTML, at least return only the contents of the form, and not the form tag itself. Then you can use
html()
to set the form content; by not replacing the form tag itself, you don't have to worry about re-binding thesubmit
event on it.)我以为 livequery 确实处理了提交事件,但是当我也遇到问题时,我想我也可以 live() 。
当我尝试绑定到提交按钮的单击事件时,我在查找表单时也遇到了问题。
当我在萤火虫中检查我的提交按钮时,我意识到它没有显示为我的表单的子项。这让我想到,如果 firebug 无法弄清楚我的提交按钮是我表单的子元素,那么 jQuery 遇到了什么样的麻烦(也许 W3C 验证器抱怨是有原因的)。
因此,我在 div 标签内的不同表单上尝试了与上面相同的代码,并且它起作用了,所以我很快从这个问题的表单中删除了表标签,令人惊讶的是它也起作用了。
我的解决方案是花额外的时间使我的 html 有效,并使用在 jQuery 1.3.3 之前不应该工作的内容,http://docs.jquery.com/Release:jQuery_1.3.2。
下面是我当前的代码,我需要弄清楚如何正确标记表单。
$('form.edit_color').live('提交', function(){
var 形式 = $(this);
var colorRow = $(this).closest('div.color_form');
var action = $(form).attr('action');
var formData = $(form).serialize();
$(colorRow).fadeOut();
$.post(动作,表单数据,
函数(数据){
$(表单).replaceWith(数据);
$(colorRow).fadeIn();
});
返回假;
});
I was thinking livequery did handle submit events, but when I was having problems with that as well, I figured I would just live() as well.
I was also having problems finding the form, when I tried to bind to the click event of the submit button.
As I was inspecting my submit button in firebug I realized the it was not being shown as a child of my form. That got me thinking that if firebug couldn't figure out that my submit button was a child element of my form, what kind of trouble was jQuery having (and maybe the W3C validator was complaining for a reason).
So I tried the same code as above on a different form that I had inside a div tag instead, and it worked, so I quickly removed the table tags from form for this question and amazingly it worked too.
My solution will be to take the extra time to make my html valid, and to go with what is not suppose to work until jQuery 1.3.3, http://docs.jquery.com/Release:jQuery_1.3.2.
Below is my current code, I will need to figure out to properly markup the form.
$('form.edit_color').live('submit', function(){
var form = $(this);
var colorRow = $(this).closest('div.color_form');
var action = $(form).attr('action');
var formData = $(form).serialize();
$(colorRow).fadeOut();
$.post(action, formData,
function(data) {
$(form).replaceWith(data);
$(colorRow).fadeIn();
});
return false;
});
试试这个。
更多信息: http://api.jquery.com/event.preventDefault/
干杯,
http://hiteshjoshi.com
Try this.
More at : http://api.jquery.com/event.preventDefault/
Cheers,
http://hiteshjoshi.com