【问题标题】:How to set ajax response in simple_form select?如何在 simple_form 选择中设置 ajax 响应?
【发布时间】:2013-03-04 03:05:17
【问题描述】:

我有一个负责获取所选州的一些城市的 ajax 请求。所以,我有一个使用 simple_form 的表单和两个选择,州和城市,如下所示:

我不知道如何替换城市选择的城市。我就是这样做的:

Controller ajax method
def by_state
   cities = City.by_state params["state"]
   render :json => { :cities => cities }
end


Form
<%= simple_form_for @store, :html => { :class => 'store-filter' } do |f| %>
  <table align="center" cellspacing="10" cellpadding="2">
    <tr>
      <th colspan="2">Store list</th>
    </tr>
    <tr>
      <td>State:</td>
      <td>
        <%= f.input :state, :collection => State.all, :include_blank => false, :label => false, :input_html => { :id => "state", :name => "state" } %>
      </td>
    </tr>
    <tr>
      <td>City:</td>
      <td>
        <%= f.input :city, :collection => City.find(:all, :conditions => { :state_id => 1 }), :include_blank => false, :label => false, :input_html => { :id => "city", :name => "city" } %>
      </td>
    </tr>
    .
    .
    .

Ajax
$("#state").change(function() {
  var state_id = $(this).val();
  $.ajax({
    type: "GET",
    url: "cities/state/"+state_id,
    success: function(data) {
      console.log(data); //returns Object with Array of Cities
      $("#city").html(data["cities"]); //replaces to nothing city select 
    }
  });
});

所以,当我选择一个州时,城市选择被 null 取代。为什么? 谢谢!

【问题讨论】:

    标签: ruby-on-rails-3 jquery simple-form


    【解决方案1】:

    我认为不需要 AJAX。您可以从另一个下拉列表填充一个下拉列表。 这里有很多有用的信息:http://forums.asp.net/t/1431726.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多