[英]in rails when I use jquery .load() for a link_to … it opens the link in whole new tab?
我正在嘗試基於https://medium.com/@adnama.lin/live-messaging-with-rails-5-action-cable-7f009e0c1d8b創建一個聊天應用程序,因此當用戶單擊用戶鏈接時,打開新頁面以將他帶到聊天視圖,我正在嘗試將視圖加載到div中,因此希望您有所幫助並隨時提出要求...
更新: $(".chat").load($(this).attr("href"));
將無法正常工作,因為我想用它的data-method = post
加載整個鏈接,諸如此類...
<% @users.each do |other_user| %>
<div> <%= link_to(user_chats_path(current_user, :other_user => other_user.id), method: :post, :class => "link") do %>
<%= other_user.username %>
<% end %>
<% end %>
這是jquery腳本:
<script>
$(".link").on("click",function {
$(".chat").load($(this));
return false;
});
</script>
load()
需要一個url字符串,但是您正在將jQuery對象傳遞給它。 從鏈接獲取href並使用它。
您還需要防止瀏覽器使用event.preventDefault()
跟隨該鏈接
嘗試更改為
$(".link").on("click",function(event) {
event.preventDefault()
$(".chat").load(this.href);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.