2010-08-03 47 views
2

在早期学习如何实现不显眼的js(使用Rails & jQuery)我遇到了如何(重新)绑定在加载(加载)包含表单的新内容后提交按钮/表单。如何(重新)绑定提交按钮/形式上。(编辑)形式

我已经设置了“创建新项目”的形式,在项目的连城的顶部显示(当用户点击一个按钮创建)

sale_case /显示/ _requirments_new.html.haml:

- form_for ([@sale_case, @sale_case_requirement]), :html => { :id => 'requirement_form', :class => "submit-with-ajax"} do |f| 
... 
%button{ :type => "submit" } Save Requirement 

的application.js:

jQuery(document).ready(function() { 
    jQuery(".submit-with-ajax").submitWithAjax(); 
} 
... 
jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
}) 

jQuery.fn.submitWithAjax = function() { 
    this.submit(function() { 
    jQuery.post(this.action, jQuery(this).serialize(), null, "script"); 
    return false; 
    }) 
    return this; 
}; 

requirements_controller.rb:

def create 
    @sale_case_requirement = @sale_case.requirements.new(params[:requirement]) 
     if @sale_case_requirement.save 
     @message = "Success..." 
     @success = true 
    #... 
    respond_to do |format| 
    format.js 
    end 
end 

要求/ create.js.erb:

mesg("<%= @message %>"); 
<%- if @success %> 
    jQuery("#requirement_form")[0].reset(); 
    jQuery('#requirements').load('<%= requirements_view_sale_case_requirements_path(@sale_case.id) %>'); 
<% end %> 

这一切都运作良好,第一次就悄悄地。问题出现在用户创建第二个项目时(通过ajax加载的表单上)。 该按钮没有绑定到submitWithAjax函数。如何在加载内容时如何做到这一点?

我最终不得不在部分(突兀)做到这一点,为了让它工作,但它让我不知道这一点。 :

%button{ :type => "submit", :onclick => "jQuery.post('#{sale_case_requirements_path(@sale_case.id, @sale_case_requirement.id)}', jQuery('#requirement_form').serialize(), null, 'script'); return false;"} Save Requirement 

回答

1

改变这种原始代码:

jQuery(document).ready(function() { 
    jQuery(".submit-with-ajax").submitWithAjax(); 
} 

这样:

jQuery(document).ready(function() { 
    jQuery(".submit-with-ajax").live('submit', submitWithAjax); 
} 

这个原代码:

jQuery.fn.submitWithAjax = function() { 
    this.submit(function() { 
    jQuery.post(this.action, jQuery(this).serialize(), null, "script"); 
    return false; 
    }) 
    return this; 
}; 

这样:

jQuery.fn.submitWithAjax = function() { 
    jQuery.post(this.action, jQuery(this).serialize(), null, "script"); 
    return false; 
}; 

jQuery .live()调用将命名处理程序绑定到与给定选择器匹配的所有当前和将来元素上的命名事件,包括通过ajax加载的元素。希望这可以做你正在寻找的东西。

查看文档以下的.live()函数:http://api.jquery.com/live/

+0

太棒了。非常感谢您的时间。(我最近从堆栈溢出中获益颇多,我试图参与并帮助回馈。)查看我的回答,了解我遇到的问题以及我最终做了什么。 – 2010-08-03 18:54:07

1

这里是我结束了在基于安德的输入代码改变。我得到一个错误与发现“submitWithAjax”与他的版本的代码,但他向我指出了正确的方向和阅读.live文档后,我想出了这个其中工程:

的application.js

jQuery(document).ready(function() { 

    # Just put it all in one place 
    jQuery(".submit-with-ajax").live('submit', function() { 
     jQuery.post(this.action, jQuery(this).serialize(), null, "script"); 
     return false; 
     }); 
... 
} 

sale_case /显示/ _requirments_new.html.haml:

# Change the button back to unobtrusive 
%button{ :type => "submit"} Save Requirement  

谢谢!

+0

是的,经过审查,我很确定在.live()调用中的函数名称后面有parens导致了您提出的错误。 – Ender 2010-08-03 19:06:45