动态添加的 HTML 中的原型 Ajax.Autocompleter 不起作用

发布于 2024-09-28 21:12:18 字数 1499 浏览 0 评论 0原文

嘿,这是我第一次真正写完问题而没有在这个过程中给我答案。 :)

我有一个嵌套表单(沿着 Ryan Bates 关于该主题的 Railscast 教程的内容),它允许用户动态添加其他字段,以使用基于原型的 javascript 添加/删除嵌套模型。可以添加和删除额外的字段,并且可以很好地创建新模型。

问题是,其中一个字段使用了 Ajax.Autocompleter;渲染部分时页面中的代码如下所示:

<script type="text/javascript">
//<![CDATA[
new Ajax.Autocompleter(...various args...);
//]]>
</script>

如果首先渲染部分(例如,如果表单以部分的一个实例开始,或者在编辑现有的嵌套模型时),则自动完成器可以正常工作。但是,它在动态添加的表单字段中不起作用。

我尝试过使用 insert() 并仅设置空 div 的innerHTML 来插入 HTML 部分。使用 insert() 时,会评估自动完成程序代码,并且

我怀疑这与 evalScripts 的工作方式有关,并且我找到了 一些有关subject,但我很难弄清楚如何将其应用于自动完成器声明。

非常感谢任何建议!

预计到达时间:添加用于在新部分中添加的 javascript:

  function add_section(link, nested_model_name, content) {
      // get the right new_id which should be in a div with class "last_id" at the bottom of 
      // the nearest section
      var last_id = parseInt($(link).up().previous('.last_id').innerHTML);
      var new_id = last_id + 1;
      var regexp = new RegExp("new_" + nested_model_name, "g");
      content = content.replace(regexp, new_id)

      // this is the line that actually inserts the content into the page
      $(link).up().insert({before: content});
  }

在插入之前,使用 firebug 进行调试显示内容是正确的(即其中包含正常工作的自动完成代码)。

Hey, this is the first time I've actually finished writing up a question without SO giving me the answer in the process. :)

I've got a nested form (along the lines of Ryan Bates' Railscast tutorial on the topic) which allows users to dynamically add additional fields for adding/removing nested models using Prototype-based javascript. The extra fields can be added and removed and work just fine to create new models.

The problem is, one of the fields uses an Ajax.Autocompleter; the code in the page when the partial is rendered looks like this:

<script type="text/javascript">
//<![CDATA[
new Ajax.Autocompleter(...various args...);
//]]>
</script>

The autocompleter works fine if the partial is rendered to begin with (eg, if the form starts out with one instance of the partial, or when editing existing nested models). However, it doesn't work in the dynamically added form fields.

I have tried using both insert() and just setting the innerHTML of an empty div to insert the HTML partial. With insert(), the Autocompleter code is evaluated and everything in the <script> tag vanishes (as I understand is expected), but the autocomplete does not work. With setting the innerHTML, the script appears exactly as it does in the pre-rendered partial, but the autocompleter still doesn't get invoked.

I suspect that this has something to do with how the evalScripts works, and I've found some documentation on the subject, but I'm having a hard time working out how to apply it to the Autocompleter declaration.

Any advice hugely appreciated!

ETA: adding the javascript used to add in the new section:

  function add_section(link, nested_model_name, content) {
      // get the right new_id which should be in a div with class "last_id" at the bottom of 
      // the nearest section
      var last_id = parseInt($(link).up().previous('.last_id').innerHTML);
      var new_id = last_id + 1;
      var regexp = new RegExp("new_" + nested_model_name, "g");
      content = content.replace(regexp, new_id)

      // this is the line that actually inserts the content into the page
      $(link).up().insert({before: content});
  }

Debugging with firebug shows that the content is correct (ie has the normally-working autocomplete code in it) before it is inserted.

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

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

发布评论

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

评论(3

來不及說愛妳 2024-10-05 21:12:18

我知道这是一个较旧的问题,但我最近遇到了同样的问题。

我不确定您使用的是哪个版本的 auto_complete?我正在使用 git 中的rails/auto_autocomplete。

在那个插件中,javascript代码是这样的,

lib/auto_complete_macros_helper.rb

def auto_complete_field(field_id, options = {})
    function =  "var #{field_id}_auto_completer = new Ajax.Autocompleter("

我遇到了同样的问题。输入字段将可见,但自动完成功能不起作用。事实证明,var 关键字使变量具有局部作用域。

这意味着 evalscripts 中计算的任何内容都位于与原始 DOM 对象不同的范围内。

我的补丁只是删除 var 关键字。

lib/auto_complete_macros_helper.rb

def auto_complete_field(field_id, options = {})
    function =  "#{field_id}_auto_completer = new Ajax.Autocompleter("

我对你的设置了解不够,无法确定出了什么问题,但我敢打赌这也与 evalscripts 的范围有关。

I know this is an older question but I had the same problem recently.

I'm not sure what version of auto_complete you are using? I was using the rails/auto_autocomplete from git.

In that plugin the javascript code was like this

lib/auto_complete_macros_helper.rb

def auto_complete_field(field_id, options = {})
    function =  "var #{field_id}_auto_completer = new Ajax.Autocompleter("

I was having the same problem. The input fields would be visible but the autocomplete functionality didn't work. It turned out that the var keyword makes the varaible local scope.

That meant anything evaluated in evalscripts was at a different scope than the original DOM object.

My patch was to just remove the var keyword.

lib/auto_complete_macros_helper.rb

def auto_complete_field(field_id, options = {})
    function =  "#{field_id}_auto_completer = new Ajax.Autocompleter("

I dont know enough about your setup to say for sure whats wrong, but I bet it has something to do with the scope of evalscripts as well.

裂开嘴轻声笑有多痛 2024-10-05 21:12:18

你的JS是在你通过ajax.updater加载的脚本标签中注册自动完成器吗?如果是这样,您必须添加 evalScript 参数以对其进行评估:new Ajax.Updater('target',url,{method:'get', evalScripts:true}) ;

is your JS to register the autocompleter within your script-tag you're loading via ajax.updater? if so, you have to add the evalScript parameter to have it eval'ed: new Ajax.Updater('target',url,{method:'get', evalScripts:true});

最舍不得你 2024-10-05 21:12:18

好的,作为一个实验,我尝试按如下方式更改我的自动完成器帮助程序代码,现在它工作正常:

FROM:

javascript_tag("new Ajax.Autocompleter(...

TO:

javascript_tag("var autocomplete_for_#{fieldname} = new Ajax.Autocompleter(...

这是在 Rails 帮助程序中,但除非我误解,我认为重点是通过声明一个变量,它导致原型在插入代码时实际评估新调用。

(不过,对于为什么这个有效而另一个无效的更多/更好的见解仍然是非常受欢迎的,因为现在这主要是盲目的运气。)

Okay, so as an experiment I tried just changing my Autocompleter helper code as follows and now it works fine:

FROM:

javascript_tag("new Ajax.Autocompleter(...

TO:

javascript_tag("var autocomplete_for_#{fieldname} = new Ajax.Autocompleter(...

This is in in a Rails helper, but unless I misunderstand, I think the point is that by declaring a variable, it's causing prototype to actually evaluate the new call as it inserts the code.

(Any more/better insight into why this works and the other doesn't would still be v welcome though, since right now this was mostly blind luck.)

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