当通过ajax加载表单时,jQuery的live和livequery对我不起作用

发布于 2024-08-13 02:20:27 字数 770 浏览 5 评论 0原文

我之前问过类似的问题 如何在插入元素后让 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 技术交流群。

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

发布评论

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

评论(5

中二柚 2024-08-20 02:20:27

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.

听风吹 2024-08-20 02:20:27

正如您在原始问题中所回答的,live 根本不适用于 submit 事件。

另请注意,回调函数中的 $(colorRow).fadeIn(); 不可能起作用,因为它引用 submit 发生。但您已在前面的 replaceWith 调用中完全删除了该 colorRow 并将其替换为新的 colorRow。替换后,formcolorRow 变量不会指向任何有用的内容。

live 是一种 hack,与事件在 HTML 中的实际工作方式不太相符。尽量避免使用它。用新标记替换页面的大部分内容通常是一个糟糕的举动。如果您可以从 JSON 返回值更新表单中现有元素的内容,而不是传回 HTML 的 katamari:那么您将无需担心重新绑定事件或保留对现已失效的 DOM 对象的引用。

(如果一定要传回HTML,至少只返回表单的内容,而不是表单标签本身。那么你可以使用html()来设置表单内容;通过不替换表单标签本身,您不必担心重新绑定 submit 事件。)

As answered in your original question, live simply does not work with the submit 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 time submit occurred. But you have completely removed and replaced that colorRow with a new one in the preceding replaceWith call. After the replace, the form and colorRow 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 the submit event on it.)

兔姬 2024-08-20 02:20:27

我以为 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();
});
返回假;
});

<div class="color_form">
<% form_for color, :url => admin_color_path(color) do |f| -%>
 <div style="background: #<%= color.value %>"></div>
  <div><%= f.text_field :title %></div>
   <div><input type="text" size="30" name="color[value]" class="colorpickerfield" value="<%= color.value %>" /></div>
    <div style="text-align:left">
     <% Color.types.each do |type, name| %>
      <div>
       <%= color_types_checkbox(color, type) %>
       <%= name %>
      </div> 
     <% end %>
    </div>
   <div>
  <%= f.submit "Update", :class => 'submit' %>
 </div>
<% end %>
</div>

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;
});

<div class="color_form">
<% form_for color, :url => admin_color_path(color) do |f| -%>
 <div style="background: #<%= color.value %>"></div>
  <div><%= f.text_field :title %></div>
   <div><input type="text" size="30" name="color[value]" class="colorpickerfield" value="<%= color.value %>" /></div>
    <div style="text-align:left">
     <% Color.types.each do |type, name| %>
      <div>
       <%= color_types_checkbox(color, type) %>
       <%= name %>
      </div> 
     <% end %>
    </div>
   <div>
  <%= f.submit "Update", :class => 'submit' %>
 </div>
<% end %>
</div>
铁憨憨 2024-08-20 02:20:27
$('form.edit_color').live("submit",
function( event ){
event.preventDefault();
}
);

试试这个。
更多信息: http://api.jquery.com/event.preventDefault/

干杯,
http://hiteshjoshi.com

$('form.edit_color').live("submit",
function( event ){
event.preventDefault();
}
);

Try this.
More at : http://api.jquery.com/event.preventDefault/

Cheers,
http://hiteshjoshi.com

如歌彻婉言 2024-08-20 02:20:27
$('form').live('submit', function(e) {
    alert(e.type);
    e.preventDefault();
});

$('form :submit').live('click', function(e) {
    $(this.form).submit();
    e.preventDefault();
});
$('form').live('submit', function(e) {
    alert(e.type);
    e.preventDefault();
});

$('form :submit').live('click', function(e) {
    $(this.form).submit();
    e.preventDefault();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文