我正在尝试创建动态选择,它开始以某种奇怪的方式工作。javascript 插入模板 html 的内容,而不是 select 标签的新选项
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}",
//orther options ...
success: function(data){
site_id : $('#sites_select').val()
$("#floors_select").html(data);
}
});
});
$("#floors_select").change(function(){
$.ajax({
url: "#{update_pods_path}",
//orther options ...
success: function(data){
floor_id : $('#floors_select').val()
$("#pods_select").html(data);
}
});
});
});
</script>
来源:
<select id="floors_select" name="[floor_id]">
<title>ELS Reservation Tool</title>
<link href="/assets/style.css?body=1" media="screen" rel="stylesheet" type="text/css">
<meta content="authenticity_token" name="csrf-param">
<meta content="3F+WpxiRZx+7u9POFOgeRHZ0QlhMrs3ccLnvUgjodio=" name="csrf-token"> etc...
devices_controller.rb
#for dynamic select
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")
respond_to do |format|
format.js
end
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")
respond_to do |format|
format.js
end
end
<title>ELS Reservation Tool</title>
<meta http-equiv="refresh" content="10000; URL=http://els-hq-reserve:3000/">
<link href="/assets/style.css?body=1" media="all" rel="stylesheet" type="text/css">
<meta content="authenticity_token" name="csrf-param">
<meta content="3F+WpxiRZx+7u9POFOgeRHZ0QlhMrs3ccLnvUgjodio=" name="csrf-token">
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<div id="header">
<img src="/images/elspmrt.png">
</div>
<div id="navigation">
<ul>
<li><a href="#">Saved Views</a></li>
<li><a href="#">Power Group</a></li>
<li><a href="#">Reserved</a></li>
</ul>
</div>
<div id="content-container1">
<div id="content">
<div class="container">
<h2>Search</h2>
<form accept-charset="UTF-8" action="/devices" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div>
<label for="label">Super Search:</label>
<input id="superstring" name="superstring" type="text">
<input name="commit" type="submit" value="Search">
</form>
路线.rb
get 'devices/update_floors', :as => 'update_floors'
get 'devices/update_pods', :as => 'update_pods'
root :to => "devices#index"
update_floors.js.erb
# app/views/devices/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
# app/views/home/update_pods.js.erb
$('#pods_select').html("<%= escape_javascript(options_for_select(@pods)) %>");
任何建议将不胜感激,谢谢你,D。