RJS 错误:TypeError:元素为 null
我在使用 ajax 时收到 RJS 错误:TypeError: element is null。
视图中使用了
<%= periodically_call_remote(:url=>{:action=>'get_user_list', :id=>'1'},
:frequency => '5') %>
的控制器
render :update do |page|
page.replace_html 'chat_area', :partial => 'chat_area', :object => [@chats, @user] if @js_update
end
我在部分 chat_area
<% if [email protected]? && !show_div(@chats).blank?%>
<% show_div_id=show_div(@chats) %>
<% for chat in @chats %>
<div class="popup" id="chat_area_<%= chat.id %>"
style="display:<%= (chat.id == show_div_id)? 'block' : 'none' %>;">
<% form_remote_for(:chat, :url => {:controller=>'chats',
:action=>'create', :id=>1}, :html=>{:name => "form_#{chat.id}"},
:complete=>"resetContent('#{chat.id}');") do |f| %>
<div style="display:none;">
<%= f.hidden_field :sessionNo, :value=>chat.sessionNo %>
<%= f.text_area :chatContent, :id=>
"chatContent_field_#{chat.id}", :cols=>"100", :rows=>"6",
:onKeyPress=>"return submitenter(this,event);" %>
</div>
<input type="image" src="images/chat/send-hover.png"
value="Send" onclick="return submit_button('<%= chat.id %>')"/>
<% end %>
</div>
</div>
<% end %>
<% else %>
<div class="popup" id="chat_area_none" style="display:'block';">
<input type="image" disabled ="disabled"
src="images/chat/send.png" style="cursor:default;" value="Send" />
</div>
<% end %>
我的 div 存在于 index.html.erb
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody><tr>
<td align="left" width="80%" valign="top" style="">
<%= text_area :chat, :chatContent, :id=> "chatContent_field", :cols=>"100", :rows=>"6" %>
</td>
<td align="left" width="20%" valign="bottom" style="padding-left:10px;padding-left:10px;x" >
<div id="chat_area">
<%= render :partial => 'chat_area' %>
</div>
</td>
</tr>
</tbody>
</table>
任何帮助表示赞赏。
问候,
萨利尔·盖克瓦德
I got RJS error: TypeError: element is null while using ajax.
I used in view
<%= periodically_call_remote(:url=>{:action=>'get_user_list', :id=>'1'},
:frequency => '5') %>
in controller
render :update do |page|
page.replace_html 'chat_area', :partial => 'chat_area', :object => [@chats, @user] if @js_update
end
in partial chat_area
<% if [email protected]? && !show_div(@chats).blank?%>
<% show_div_id=show_div(@chats) %>
<% for chat in @chats %>
<div class="popup" id="chat_area_<%= chat.id %>"
style="display:<%= (chat.id == show_div_id)? 'block' : 'none' %>;">
<% form_remote_for(:chat, :url => {:controller=>'chats',
:action=>'create', :id=>1}, :html=>{:name => "form_#{chat.id}"},
:complete=>"resetContent('#{chat.id}');") do |f| %>
<div style="display:none;">
<%= f.hidden_field :sessionNo, :value=>chat.sessionNo %>
<%= f.text_area :chatContent, :id=>
"chatContent_field_#{chat.id}", :cols=>"100", :rows=>"6",
:onKeyPress=>"return submitenter(this,event);" %>
</div>
<input type="image" src="images/chat/send-hover.png"
value="Send" onclick="return submit_button('<%= chat.id %>')"/>
<% end %>
</div>
</div>
<% end %>
<% else %>
<div class="popup" id="chat_area_none" style="display:'block';">
<input type="image" disabled ="disabled"
src="images/chat/send.png" style="cursor:default;" value="Send" />
</div>
<% end %>
My div present in index.html.erb
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody><tr>
<td align="left" width="80%" valign="top" style="">
<%= text_area :chat, :chatContent, :id=> "chatContent_field", :cols=>"100", :rows=>"6" %>
</td>
<td align="left" width="20%" valign="bottom" style="padding-left:10px;padding-left:10px;x" >
<div id="chat_area">
<%= render :partial => 'chat_area' %>
</div>
</td>
</tr>
</tbody>
</table>
Any help is appreciated.
Regards,
Salil Gaikwad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现为什么会发生这种情况,因为我没有正确关闭“chat_area”上方的 div 。
我做的唯一一件事就是关闭 div,它就像魔术一样工作,
再次感谢所有
Reagrds,
Salil Gaikwad
I find it out why it happens because I didn't close div above the "chat_area" properly.
the only thing i did isclose the div and it works like a magic,
neways thanks to all
Reagrds,
Salil Gaikwad
这意味着 RJS 正在尝试操纵 html 输出中不存在的内容。
看起来问题是您想要更新您的
user_list
但您的 html 中不存在具有该 id 的任何内容。确保您正在渲染该内容,并确保将periodically_call_remote
方法中的update
属性设置为要更新的元素的 ID。This means that RJS is attempting to manipulate something in html output that doesn't exist.
It looks like the problem is that you want to update your
user_list
but nothing with that id exists in your html. Make sure you are rendering that, also make sure you have theupdate
attribute in yourperiodically_call_remote
method set to the id of the element you want to update.