PLease帮助,我用了一些例子来建立动态选择,但它似乎不工作,尽管如此,在控制台我可以看到200状态,我不流利,所以请帮助,问题是列表中的选择不会减少,它显示相同的选项后选择以前
index.html.erb
<%= collection_select(nil, :site_id, @sites, :id, :name, {:prompt => "Select a Site"}, { :id => "sites_select"}) %>
<br/>
<%= collection_select(nil, :floor_id, @floors, :id, :name, {:prompt => "Select a Floor"}, {:id => "floors_select"}) %>
<br/>
<%= collection_select(nil, :pod_id, @pods, :id, :name, {:prompt => "Select a pod"}, {:id => "pods_select"}) %>
<script>
$(document).ready(function() {
$('#sites_select').change(function() {
$.ajax({
url: "<%= update_floors_path %>",
data: {
site_id : $('#sites_select').val()
},
dataType: "script"
});
});
$('#floors_select').change(function() {
$.ajax({
url: "<%= update_pods_path %>",
data: {
floor_id : $('#floors_select').val()
},
dataType: "script"
});
});
});
</script>
devices_controller.rb
def update_floors
# updates floors and pods based on genre selected
site = Site.find(params[:site_id])
# map to name and id for use in our options_for_select
@floors = site.floors.map{|a| [a.name, a.id]}.insert(0, "Select a Floor")
@pods = site.pods.map{|s| [s.name, s.id]}.insert(0, "Select a Pod")
end
def update_pods
# updates pods based on floor selected
floor = Floor.find(params[:floor_id])
@pods =floor.pods.map{|s| [s.name, s.id]}.insert(0, "Select a Pod")
end
update_floors.js.erb
$('#floors_select').html("<%= escape_javascript(options_for_select(@floors)) %>");
$('#pods_select').html("<%= escape_javascript(options_for_select(@pods)) %>");
update_pods.js.erb $('#pods_select').html("<%= escape_javascript(options_for_select(@pod)) %>");
routes.rb
get 'devices/update_floors', :as => 'update_floors'
get 'devices/update_pods', :as => 'update_pods'
root :to => "devices#index"
谢谢你的建议,谢谢你,D
发布于 2012-12-12 15:45:22
新加载的JavaScript似乎没有向父页面添加效果。为什么不在Ajax成功回调中使用$().html。并使update.js.html页面只包含select选项。应该行得通。
javascript:
$("#sites_select").change(function(){
$.ajax({
url: your_url,
//orther options ...
success: function(data){
$("#floors_select").html(data);
}
});
});
在你的update_floors.js.html中
<%=options_for_select(@floors)%>
对更新pod执行相同的操作。:)
发布于 2012-12-12 15:39:40
问题出在你的控制器,
您在代码中的任何地方都没有提到respond_to js块,
只需将此代码放入两个更新方法中,
respond_to do |format|
format.js
end
发布于 2012-12-12 15:48:23
在控制器respond_to :js, :only=>[:update_floors,:update_pods ]
中给出响应格式
除此之外,将以下内容写入update_floors.js.erb
文件。
$('#floors_select').html("<%= escape_javascript(collection_select(:your_object_name, :floor_id, @floors.to_a, :id, :name, :prompt=>"Select a Floor")) %>");
我认为上面的方法是可行的。
https://stackoverflow.com/questions/13834867
复制相似问题