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

31 篇文章 0 订阅

一: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值