Rails 2 Replace_html 将 javascript 放入页面中
在我的控制器中,我有:
respond_to do |format|
format.js
end
在我的 rjs 中:
page.replace_html("source_credential_list", :partial => "source_credential_list", :object => @all_source_credentials)
或
page.replace("source_credential_list", :partial => "source_credential_list", :object => @all_source_credentials)
在我的部分中:(
<% fields_for(@brand_report_source_credential_key) do |ff| %>
<%= ff.select :source_credential_id , options_from_collection_for_select(@all_source_credentials,'id','credential_display_name')%>
<% end %>
在我的原始视图中:
<span id="source_credential_list">
<%= render :partial => 'source_credential_list' %>
</span>
当我替换它时效果很好,但只有一次,第二次我收到带有 null 的警报,我怀疑是因为跨度如果我执行replace_html,它会起作用,它会更新跨度,但会将实际的rjs/javascript放入跨度中,包括新的选择/下拉
列表 。 尝试 { Element.update("source_credential_list", "\n \n\n"); } catch (e) { alert('RJS 错误:\n\n' + e.toString()); alert('Element.update(\"source_credential_list\", \"\n \n\n\");'); 。
我尝试将跨度移至部分并执行两个组合,但没有任何效果 我确信这是显而易见的事情......
In my controller I have:
respond_to do |format|
format.js
end
In my rjs:
page.replace_html("source_credential_list", :partial => "source_credential_list", :object => @all_source_credentials)
or
page.replace("source_credential_list", :partial => "source_credential_list", :object => @all_source_credentials)
In my partial: (
<% fields_for(@brand_report_source_credential_key) do |ff| %>
<%= ff.select :source_credential_id , options_from_collection_for_select(@all_source_credentials,'id','credential_display_name')%>
<% end %>
In my orig view:
<span id="source_credential_list">
<%= render :partial => 'source_credential_list' %>
</span>
When I do replace it works great, but only one time, the 2nd time I get a alert with a null, I suspect because the span is gone. If I do replace_html, it kinda works, it updates the span but it puts the actual rjs/javascript into the span including the new select/drop down. So something like this:
Source credential
try { Element.update("source_credential_list", "\n \n\n"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"source_credential_list\", \"\n \n\n\");'); throw e }
I tried moving the span into the partial and doing both combos, but nothing working. Im sure its something obvious....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定哪段代码触发了此操作,但假设它是由 link_to_remote 调用触发的,请确保您没有 :update =>; link_to_remote 调用中的“source_credential_list”参数 - 否则它将执行 RJS 并更新使用 javascript 代码指定的元素。
请参阅此处: http://railsforum.com/viewtopic.php?pid=44191#p44191< /a>
另外,当您执行 page.replace 时,它只会工作一次,因为 page.replace 会替换指定的元素,包括周围的标记,因此除非您指定的部分包含 span 标记,否则它'第一次使用时就会消失。
I'm not sure what piece of code is triggering this action, but assuming it's triggered by a link_to_remote call, make sure you don't have an :update => "source_credential_list" parameter in your link_to_remote call -- otherwise it will both execute the RJS and update the element specified with the javascript code.
See here: http://railsforum.com/viewtopic.php?pid=44191#p44191
Also, when you do page.replace, it only works once because page.replace replaces the specified element, including the surrounding tags, so unless the partial you specify includes the span tag, it'll disappear the first time it's used.