Rails 自动完成功能适用于父级形式,但不适用于嵌套形式。为什么?

发布于 2025-01-07 14:51:03 字数 1824 浏览 0 评论 0原文

在带有 JEE 的 Rails 3.2 应用程序中,我尝试使用 Cacoon 在复杂的嵌套表单中实现一个像 Railscast #102 那样的自动完成字段。

我已经设法让类似的自动完成字段在父表单上完美运行。但是我无法让自动完成功能在嵌套表单上工作。看来 javascript 没有被触发。

我的表单看起来像

<%= form_for @parent do |f| %>
<%= f.text_field :name, :class=>"parent-name", :data => {:autocomplete_source => Model.order(:name).map(&:name) } %>
  <%= f.fields_for :children  do |child| %>
    <%= f.fields_for :grand_children do |grand_child| %>
      <%= f.text_field :name, :class=>"grand-child-name", :data => {:autocomplete_source => Model.order(:name).map(&:name) } %>
    <% end %>
  <% end %>
<% end %>

我有两个 javascript 函数

$(function() {
    $('.parent-name').autocomplete({
        source: $('.parent-name').data('autocomplete-source')
    });
});

$(function() {
    $('.grand-child-name').autocomplete({
        source: $('.grand-child-name').data('autocomplete-source')
    });
}); 

父自动完成功能运行良好,而孙子则不然。我错过了什么吗?是否有原因导致这在嵌套形式中不起作用?或者我的方法犯了错误?

感谢任何指点。

编辑

孙子元素是异步添加的,父元素是在页面加载时呈现的。这似乎是问题的关键。当我打开现有记录进行编辑(即在页面加载时呈现和填充字段)时,自动完成功能将在这些字段上运行。

解决方案

感谢 Chris Drappier 为我指明了正确的方向。他说我需要在添加新的表单元素时添加我的观察者。

我现在有两个 jquery 函数。原来的

$(function() {
    $('.grand-child-name').autocomplete({
        source: $('.grand-child-name').data('autocomplete-source')
    });
});

$(function() {
    $(document).on("focus",".grand-child-name", function() {
        $('.grand-child-name').autocomplete({
            source: $('.grand-child-name').data('autocomplete-source')
        }); 
    });
})

Chris 向我指出了 .live() 函数,但显然它正在被贬值,而 .on(focus) 提供了正确的功能。

谢谢克里斯!

in a Rails 3.2 app with JEE, I am trying to implement an autocomplete field a la Railscast #102, within a complex nested form using Cacoon.

I have managed to get similar autocomplete field working perfectly on the parent form. However I can not get the autocomplete to work on the nested form. It appears that the javascript is not being triggered.

My form looks something like

<%= form_for @parent do |f| %>
<%= f.text_field :name, :class=>"parent-name", :data => {:autocomplete_source => Model.order(:name).map(&:name) } %>
  <%= f.fields_for :children  do |child| %>
    <%= f.fields_for :grand_children do |grand_child| %>
      <%= f.text_field :name, :class=>"grand-child-name", :data => {:autocomplete_source => Model.order(:name).map(&:name) } %>
    <% end %>
  <% end %>
<% end %>

I have two javascript functions

$(function() {
    $('.parent-name').autocomplete({
        source: $('.parent-name').data('autocomplete-source')
    });
});

$(function() {
    $('.grand-child-name').autocomplete({
        source: $('.grand-child-name').data('autocomplete-source')
    });
}); 

The parent autocomplete is working perfectly, the grandchild is not. Am I missing something? Is there a reason that this would not work in a nested form? Or have I made a mistake in my approach?

Grateful for any pointers.

EDIT

The grand-child elements is being added asynchronously, the parent element is being rendered on page load. This appears to be the key to the issue. When I open an existing record to edit (i.e. fields are rendered and populated on page load) the autocomplete is functioning on these fields.

SOLUTION

Thanks to Chris Drappier who pointed me in the right direction. He said I need to add my observer when adding the new form element.

I now have two jquery functions. The original

$(function() {
    $('.grand-child-name').autocomplete({
        source: $('.grand-child-name').data('autocomplete-source')
    });
});

and

$(function() {
    $(document).on("focus",".grand-child-name", function() {
        $('.grand-child-name').autocomplete({
            source: $('.grand-child-name').data('autocomplete-source')
        }); 
    });
})

While Chris pointed me towards the .live() function, apparently this is being depreciated, and .on(focus) provides the correct functionality.

Thanks Chris!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

北音执念 2025-01-14 14:51:03

这是一个棘手的问题,但我相信问题是当您添加新的表单元素时也必须添加观察者。如果您使用 jquery,则有处理这种情况的功能。请参阅 jquery 中的 api 文档 live() 。我认为这应该能让你到达你需要去的地方。

This is a tricky one, but I believe the problem is that you have to add your observer when you add the new form element as well. if you are using jquery, there is functionality to handle this case. see the api docs in jquery for live() . I think that should get you where you need to be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文