2
我试图用RJS替换DOM中的div。这里是我试图代码, 该控制器具有此方法:如何使用AJAX替换Rails 3中的div?
def change
render :update do |page|
page.replace(:test_id, :partial => "input",:locals =>{ :type => 'text', :name => 'user[user][contactinfo][city]', :val => "", :size => '244', :placeholder_text => 'Yes it is working...'})
end
end
该视图包含:
<div id = "test_id"></div>
<%= link_to "AJAX", "/poc/change", :remote => true %>
现在我想更换div id="test_id"
与提到的部分。
输出我得到的是:
try {
Element.replace("test_id", "<input type=\"text\" id=\"user[user][contactinfo][city]\" name=\"user[user][contactinfo][city]\" value=\"\" placeholder=\"Yes it is working...\" style=\"width:244px; height:33px; border:0; color:#646464; background:url(/images/form_textfield_244.png) 0 5px no-repeat; padding:12px 5px 0 5px; margin:0 0 10px 0;\" />\n");
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.replace(\"test_id\", \"<input type=\\\"text\\\" id=\\\"user[user][contactinfo][city]\\\" name=\\\"user[user][contactinfo][city]\\\" value=\\\"\\\" placeholder=\\\"Yes it is working...\\\" style=\\\"width:244px; height:33px; border:0; color:#646464; background:url(/images/form_textfield_244.png) 0 5px no-repeat; padding:12px 5px 0 5px; margin:0 0 10px 0;\\\" />\\n\");'); throw e }
这在浏览器中看到。任何人都可以解释我出错的地方吗?预期的输出是div应该替换为替换的任何东西。
RJS有什么问题? – 2011-03-22 13:59:05
@AashishP RJS被弃用和错误,因为它为你生成JS。与不显眼的JS模式,你写你自己的JS,因此你可以控制你的事件绑定。 RJS也迫使你通过线路发送JS,这是一个坏主意,因为它依赖于浏览器来正确执行JS,而JS从来不这样做。通过线路发送JSON或HTML数据更可靠,然后对'ajax:success'回调执行任何额外处理。你应该*永远不会*发送HTML和附带的内联JS在相同的响应,除非你喜欢解决'$是未定义的错误... – tubbo 2012-05-21 19:19:53
是否有无论如何也取代div的ID? – wachichornia 2012-08-03 13:48:48