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

Java代码   收藏代码
  1. 一:partial模版替换  
  2.   
  3. $(document).on('change','.change_city',function(){  
  4.       var $this = $(this),  
  5.          district_name = $this.find('.text').text();   
  6.       if (district_name != "") {  
  7.       $.ajax({  
  8.           url: "/inquiry/inquiries/change_city",  
  9.           type: "get",  
  10.           data: {  
  11.             district_name: district_name  
  12.           },  
  13.           success: function(data){  
  14.             $this.parents('.fields').find('.city').html(data);  
  15.             $(document).find('.ui.dropdown').dropdown();  
  16.           }  
  17.         });  
  18.       }  
  19.     });  
  20.  获取地址
  21. def change_city  
  22.     @cities = City.ind_cities(params[:district_name])||[]  
  23.     render :partial => "change_city"   
  24. end  
  25.   
  26. <%= select_tag "contact[inquiries][city]", options_for_select(@cities), include_blank: "City"class"ui compact dropdown" %>  
  27.   
  28.   
  29. 二:选择内容替换  
  30.   
  31. <%= f.select :province, options_for_select(City.ind_districts, company.province), include_blank: 'Select State' %>  
  32. <%= f.select :city, options_for_select({company.city => company.city}.compact || [], company.city), include_blank: 'Select City' %>  
  33.   
  34.   
  35. $("#state").change(function(){  
  36.     $.ajax({  
  37.       type: "get",  
  38.       url: '<%= get_cities_path %>',  
  39.       dataType: "json",  
  40.       data: {state: $('#state').val()},  
  41.       success: function(data){  
  42.         $("#city").html('<option value="">City </option>');  
  43.         $("#city").append(data.options);  
  44.         var select_comboSelect = $(document).find('select');  
  45.         if (select_comboSelect.length && select_comboSelect.length > 0) {  
  46.           select_comboSelect.comboSelect();  
  47.         }  
  48.       }  
  49.     });  
  50.   });  
  51.   
  52. def city  
  53.     options = City.ind_cities(params[:q].to_s.strip).inject(""){|options_str, c| options_str += "<option value='#{c[0]}'>#{c[1]}</option>"}  
  54.     render json: {options: options}  
  55. end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值