`

联动下拉框的两种实现方式

 
阅读更多
一:Ajax联动替换


$.ajax({
      type: 'get',
      data: { current_index: i ,actionname: actionname, ad_type: current_ad_type },
      url: "<%= check_ad_platform_orders_path %>",
      success: function(data) {
        $(ad_index + "_product_id_change").html(data);
        //product_id_change(i);
      },
     error: function(data) {}
    });


action

def check_ad_platform
    render :partial=>"ad_type_info", :locals => {:index => params[:current_index].to_i , :actionname => params[:actionname] , :ad_type => params[:ad_type] }
  end


_ad_type_info.html.rb

<%= select "ad_#{index}", :product_id, order_bu_product(actionname,ad_type), {:include_blank => "请选择" }, {:name=>"ad_#{index}[product_id]",:class => "select span5 adv_product_id"} %>


二:初始加载,Ajax选择处理

 #找出当前用户能看到的BU产品
  def all_ad_types
    @all_ad_types = []
    @all_product_types = current_user.send("self_bu_adtype_new")-["其他","OTHERTYPE"]
    @all_product_types.each do |product_type|
      @all_ad_types << [product_type[1],[["请选择",""]]+ current_user.send("self_bu_adcombo_new",product_type[1])]
    end
    @all_ad_types
  end

<% unless all_ad_types == [] %>
 if(current_ad_type == ""){
    $(ad_index + "_product_id option").remove();
    options = "<option value" + ">" + "请选择" + "</option>";
    $(ad_index + "_product_id").append(options);
 }else{
    var all_product_ids = <%= raw @all_ad_types %>;

    current_product_type = $.grep(all_product_ids,function(n, i){ return n[0] == current_ad_type});

    $(ad_index + "_product_id option").remove();

    product_types = $.isArray(current_product_type[0][1]) ? current_product_type[0][1] : current_product_type[0][1].split(",");
    
    options = ''
    $.each(product_types, function(index,product_type){
        options += "<option value=" + product_type[1] + ">" + product_type[0] + "</option>";
    })
    $(ad_index + "_product_id").append(options);
  }
<% end %>

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics