gpt4 book ai didi

jquery - Rails 3.2.8 应用程序中的 AJAX 不会重新加载目标 div

转载 作者:行者123 更新时间:2023-12-01 01:35:49 32 4
gpt4 key购买 nike

在我的布局文件中,我有两个下拉菜单。当选择一个(类别)时,需要更新第二个(项目)的内容。

在布局文件中:

<div id="pull_down_items" class="pull-right">
<%= render :partial => 'layouts/pull_down_items' %>
</div>
<%= render :partial => 'layouts/pull_down_category' %>

layouts/_pull_down_items.html.erb:

<%= select_tag "current_item", options_from_collection_for_select(current_category.items, "id", "item_name", session[:current_item]), :id=>"change_current_item", :remote => true %>

布局/_pull_down_category.html.erb:

<%= select_tag "current_category", options_from_collection_for_select(categories, "id", "category_name", session[:current_category]), :id=>"change_current_category", :remote => true %>

application.js 中的 jQuery:

$("#change_current_category").change(function() {
var category_id = $(this).val();
var url = "/system/" + category_id + "/change_category";
$.post(url, category_id, function(html) {
});
});

在我的系统 Controller 中:

  def change_category
unless params["id"].blank?
session[:current_category] = params["id"].to_i
current_category.reload # this is a method in application_controller.rb
end
respond_to do |format|
format.js
end
end

在change_team.js.erb中:

$("#pull_down_items").html("<%= render :partial => 'layouts/pull_down_items' %>")

结果: session [:current_category]已更改为正确的类别。项目下拉菜单 div 未重新加载。

感谢您的帮助。

最佳答案

默认情况下,jQuery.post 不保证执行它下载的内容。尝试将 dataType 参数设置为“script”,如 http://api.jquery.com/jQuery.post/ 所示。

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.

关于jquery - Rails 3.2.8 应用程序中的 AJAX 不会重新加载目标 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13896968/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com