`
open2ye
  • 浏览: 99046 次
  • 性别: Icon_minigender_1
  • 来自: 温州
社区版块
存档分类
最新评论

Rails ajax 的联动的下拉框

阅读更多


# app\controllers\examples_controller.rb
ruby 代码
 
  1. class ExamplesController < ApplicationController  
  2.   
  3.   def ajax_select  
  4.     @items = %w{AA BB}  
  5.   end  
  6.     
  7.   def get_sub_items  
  8.     if params[:item] == "AA"  
  9.       @sub_items = %w{AA1 AA2}  
  10.     else  
  11.       @sub_items = %w{BB1 BB2}  
  12.     end  
  13.       
  14.     render :partial => "select"  
  15.   end  
  16. end  
# app\views\examples\ajax_select.rhtml
xml 代码
 
  1. <%= javascript_include_tag :defaults %>  
  2.   
  3. <h1>Examples#ajax_select</h1>  
  4. <p>Find me in app/views/examples/ajax_select.rhtml</p>  
  5.   
  6. <%= select :obj, :att,  @items, {}, :onchange => remote_function(:update => "sub_items",  
  7.        :method => "get",  
  8.        :with => "'item=' + value",  
  9.        :url => { :controller => :examples, :action => :get_sub_items})   
  10.        %>  
  11. <div id="sub_items">  
  12.   <select></select>  
  13. </div>  
#app\views\examples\_select.rhtml
ruby 代码
 
  1. <%= select(:obj:item@sub_items) %>  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics