轨道3和x2B;JQuery+;动态组合框

轨道3和x2B;JQuery+;动态组合框,jquery,ruby-on-rails-3,Jquery,Ruby On Rails 3,我一直在寻找其他线程,但无法得到一个工作的例子 我有一个“组”组合框,当我选择一个值时,它会在选择该组的值时更改组合框“类别”的值 查看(show.html.erb) Javascript(application.js) $(文档).ready(函数(){ $(“#组”).更改(函数(){ $.ajax({url:':update_categories,:id=>@group_id%>', 数据:“选定=”+此.value, 数据类型:“脚本”}) }); }); 感谢您的帮助如果您不知道ra

我一直在寻找其他线程,但无法得到一个工作的例子

我有一个“组”组合框,当我选择一个值时,它会在选择该组的值时更改组合框“类别”的值

查看(show.html.erb)

Javascript(application.js)

$(文档).ready(函数(){
$(“#组”).更改(函数(){
$.ajax({url:':update_categories,:id=>@group_id%>',
数据:“选定=”+此.value,
数据类型:“脚本”})
});
});

感谢您的帮助

如果您不知道rails的内容,您可能应该发布它生成的html,请将
更改
事件处理程序附加到
组合中

$("#groupCombo").change(function(e){

  //make the ajax call to get the categories

  $.ajax({
     url:"url",
     type:"get",
     dataType:"json", //the type of response you are expecting from the server
     success:function(data){
        $("#categoryCombo").empty(); //clear the categories combo

        //iterate over the result sent by the server and populate the category combo e.g.
         $.each(data,function(k,v){
           $("#categoryCombo").append("<option>"+v+"</option>");
         });

     },
     error:function(jxhr){
         console.log(jxhr.responseText);
     }


  });

});
$(“#groupCombo”).change(函数(e){
//进行ajax调用以获取类别
$.ajax({
url:“url”,
键入:“获取”,
数据类型:“json”,//您期望从服务器得到的响应类型
成功:功能(数据){
$(“#categoryCombo”).empty();//清除类别组合
//迭代服务器发送的结果并填充类别组合,例如。
$。每个(数据、函数(k、v){
$(“#categoryCombo”)。追加(“+v+”);
});
},
错误:函数(jxhr){
log(jxhr.responseText);
}
});
});

<div id="categories" class="fields">
        <%= f.label t(:category) %><br/>
        <%= f.select :category_id, options_from_collection_for_select(@categories, :id, :name) %>
    </div>
def show
    @store_classification = StoreClassification.new    
    @groups = Group.all
    @categories = Category.all
  end

  def update_categories
    category = Category.find(:all,
                             :conditions => ['group_id = ?',params[:selected]])
    render :partial => :categories
  end
$(document).ready(function(){
    $("#groups").change(function() {
        $.ajax({url: '<%= url_for :action => :update_categories, :id => @group_id %>',
        data: 'selected=' + this.value,
        dataType: 'script'})
    });
});
$("#groupCombo").change(function(e){

  //make the ajax call to get the categories

  $.ajax({
     url:"url",
     type:"get",
     dataType:"json", //the type of response you are expecting from the server
     success:function(data){
        $("#categoryCombo").empty(); //clear the categories combo

        //iterate over the result sent by the server and populate the category combo e.g.
         $.each(data,function(k,v){
           $("#categoryCombo").append("<option>"+v+"</option>");
         });

     },
     error:function(jxhr){
         console.log(jxhr.responseText);
     }


  });

});