Javascript 如何在不重新加载页面的情况下创建远程链接以更新信息
我有个问题。。有两个链接需要更新页面上的信息而不更新它。我不擅长JavaScript,尝试了很多解决方案,但都不管用。。 一般来说,我有一个按钮“添加项目到购物车”和顶部显示的工具栏,显示购物车中的项目数量和总价值。 cart中的参数存储在会话[:cart]中,如 购物车={product\u id=>quantity,product\u id=>quantity} 当我点击“将商品添加到购物车”时,我需要在不更新页面的情况下更新上工具栏上的信息。 我有点像:Javascript 如何在不重新加载页面的情况下创建远程链接以更新信息,javascript,jquery,ruby-on-rails,ruby,ajax,Javascript,Jquery,Ruby On Rails,Ruby,Ajax,我有个问题。。有两个链接需要更新页面上的信息而不更新它。我不擅长JavaScript,尝试了很多解决方案,但都不管用。。 一般来说,我有一个按钮“添加项目到购物车”和顶部显示的工具栏,显示购物车中的项目数量和总价值。 cart中的参数存储在会话[:cart]中,如 购物车={product\u id=>quantity,product\u id=>quantity} 当我点击“将商品添加到购物车”时,我需要在不更新页面的情况下更新上工具栏上的信息。 我有点像: <%= link_to 'A
<%= link_to 'Add to cart', "/cart/#{product.id}", type: "submit",
class: "single_add_to_cart_button button alt", remote: true %>
$('.single_add_to_cart_button').bind('ajax:complete', function() {
$('#frontend-toolbar').text("<%= cart_info_quantity %>");
});
但它不会更新我购物车中的信息,所以我应该重新加载页面
购物车信息数量方式:
def cart_info_quantity
if session[:cart]
@quantity = 0
@cart = session[:cart]
@cart.each do |id, quantity|
@quantity += quantity
end
return @quantity
else
return 0
end
总价采用相同的方法:
def cart_info_total
if session[:cart]
@total = 0
@cart = session[:cart]
@cart.each do |id, quantity|
@total += quantity * Product.find_by_id(id).price
end
return @total
else
return 0
end
以及在模式窗口中显示购物车中所有产品的方法:
def cart_products
if session[:cart]
products = Hash.new
@cart = session[:cart]
@cart.each do |id, quantity|
products[Product.find_by_id(id)] = quantity
end
return products
else
return nil
end
请帮帮我,因为我很长时间都来不了 “有两个链接需要更新页面上的信息而不更新它。”你这么说是什么意思?我的意思是,我需要更新页面上的信息而不刷新页面。对不起,我的英语很糟糕。“有两个链接需要更新页面上的信息而不更新它。”你这么说是什么意思?我的意思是,我需要更新页面上的信息而不刷新页面。对不起我的英语。
def cart_info_total
if session[:cart]
@total = 0
@cart = session[:cart]
@cart.each do |id, quantity|
@total += quantity * Product.find_by_id(id).price
end
return @total
else
return 0
end
def cart_products
if session[:cart]
products = Hash.new
@cart = session[:cart]
@cart.each do |id, quantity|
products[Product.find_by_id(id)] = quantity
end
return products
else
return nil
end