0
無法弄清楚如何使用JQuery filter()方法和rails基於選定狀態的ID來過濾城市。已經觀看Railscasts#88定期和修改,但我沒有使用optgroup,所以他推薦的方式不起作用。我正在尋找的是在下面的filter()方法調用中放置什麼。我懷疑它應該是這樣的:Rails&Jquery:根據選定狀態選項篩選城市
.filter("select label='job_state_id' "))
不管怎麼說,這是JQuery的:
$(document).ready(function(){
// grab all cities for later filtering
cities = $('#job_city_id').html();
$('#job_state_id').change(function(){
state = $('#job_state_id option:selected').text();
// can't figure out how to only display cities of the selected state, hence filter() is blank below
options = $(cities).filter().text();
$('#job_city_id').html(options);
});
});
Rails的模型
class Job < ActiveRecord::Base
belongs_to :state
belongs_to :city
class State < ActiveRecord::Base
has_many :citys
has_many :jobs
class City < ActiveRecord::Base
belongs_to :state
has_many :jobs
Rails的形式
<div class="field" >
<%= f.label :state_id %>
<%= f.collection_select :state_id, State.all, :id, :name, {:prompt => '-- Select a US State --'} %>
</div>
<div class="field">
<%= f.label :city_id %>
<%= f.collection_select :city_id, City.all, :id, :name, {:prompt => '-- Select a US City --'} %>
和HTML
<div class="field" >
<label for="job_state_id">State</label>
<select name="job[state_id]" id="job_state_id"><option value="">-- Select a US State --</option>
<option value="1">California</option>
<option value="2">Nevada</option>
<option value="3">Oregon</option>
<option value="4">Alaska</option></select>
</div>
<div class="field">
<label for="job_city_id">City</label>
<select name="job[city_id]" id="job_city_id"><option value="">-- Select a US City --</option>
<option value="1">Los Angeles</option>
<option value="2">San Francisco</option>
<option value="3">Portland</option></select>
</div>
任何幫助正確的方向將不勝感激!
丹
感謝您的反饋和想法史蒂夫 - 聽起來好像有很多方法可以實現這一點! -擔 – DanHodgins