Rails呈现形式+;jQuery问题

Rails呈现形式+;jQuery问题,jquery,ruby-on-rails,ruby-on-rails-3,jquery-ui,Jquery,Ruby On Rails,Ruby On Rails 3,Jquery Ui,关于使用呈现形式的jQuery,我有一个小错误 我有链接到添加字段来添加嵌套渲染。 问题是当我添加字段时,我的jQuery停止工作。我没有错误,什么都没有,只是我的jquery停止工作了 这是我从helper和jquery获得的代码 def link_to_remove_fields(name, f) f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") end def link_to_add_

关于使用呈现形式的jQuery,我有一个小错误

我有链接到添加字段来添加嵌套渲染。 问题是当我添加字段时,我的jQuery停止工作。我没有错误,什么都没有,只是我的jquery停止工作了

这是我从helper和jquery获得的代码

def link_to_remove_fields(name, f)
  f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end 

def link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
  end   
  link_to_function(name, ("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))

end

function add_fields(link, association, content) {

var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")

$(link).parent().before(content.replace(regexp, new_id));

}
我尝试使用的jquery是来自jqueryui的日期选择器。 我的渲染:

<div class="field">
<div>
    <%= f.label :work_start_date %><br/>
    <%= f.text_field :work_start_date, :class => 'calpicker' %>
</div>
<div>
    <%= f.label :work_end_date %><br/>
    <%= f.text_field :work_end_date, :class => 'calpicker' %>
</div>
<div>
    <%= f.label :value_renovation %><br/>
    <%= f.text_field :value_renovation %>
</div>

<div>
    <%= f.label :note %><br/>
    <%= f.text_area :note, :rows => '3' %>
</div>

</div>
我看到链接到添加字段之后的嵌套渲染与从控制器(mymodel.nestedmodel.build)设置defaut视图之间的差异

在mymodel.nestedmodel.build中,我的类在calpicker中有“hasDatepicker”,在render中,我的类中没有此元素

我能做什么?用我的萤火虫,我看不出有什么错误


谢谢您的帮助。

您遇到了这个问题,因为您是在加载DOM之后和
$(“.calpicker”).datepicker()之后添加字段的已运行,因此新字段不包括在内,因此没有日期选择器

您将需要使用.live函数来实现此功能,因此请阅读本文,它可能会有所帮助:

提取和修改的源:

jQuery(function() {

  // for PikaChoose

  $(".calpicker").datepicker();

});
jQuery(function() {
    $('input.calpicker').live('click', function() {
        $(this).datepicker({showOn:'focus'}).focus();
    });
});