[英]ajax closest is not affected in ruby on rails
你好我是ruby on rails上的新手。我嘗試在我的演示網站上做一些ajax.Delete已執行但我必須每次刷新。
視圖/共享/ _feed_item.html.haml
%li{id: feed_item.id}
= image_tag current_user.image_url(:small_thumb).to_s
%span.user
%span.destination
%b Source:
= feed_item.source
%span.destination
%b Destination:
= feed_item.destination
%span.destination
%b Date:
= feed_item.date
%span.timestamp
%b Time:
= feed_item.time
%span.content
%b Comment:
= feed_item.content
%span.timestamp
- if current_user == feed_item.user
= link_to "Delete", feed_item ,method: :delete, data: { confirm: "You sure?" }, title: "Delete", remote: true, class: "delete_post"
查看/請求/ delete.js.haml
$(document).ready(function(){
$(function(){
$('.delete_post').bind('ajax:success',function() {
$(this).closest('li').fadeOut();
});
});
});
控制器/ requests_controller.rb
def destroy
@request.destroy
respond_to do |format|
format.html { redirect_to root_url, notice: "Post deleted" }
format.js { render nothing: true}
end
end
原始的HTML鏈接
提前致謝。
之所以jquery不起作用。 問題是turbolinks。 $(document).ready(function()
不使用turbolinks。更多看看這個描述
ABPrime,
如果closest
不起作用,你能嘗試使用直接jquery嗎?
:plain
$(document).ready(function(){
$('.delete_post').parent().parent().fadeOut();
});
我認為這種情況正在發生,因為這個選擇器沒有找到任何元素:
$(this).closest('li')
jquery 最接近的函數遍歷DOM,這意味着它將查看父級而不是兄弟級。 而是嘗試使用這個:
$(this).prev('li')
您總是可以在該行之前放置一個調試器語句,這樣您就可以使用幾個不同的jquery函數來獲取$(this),看看什么效果最好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.