大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Ruby on Rails – 更改事件的下拉框,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
请帮帮我.
在你看来:
<%= SELEct_tag :first_SELEct,# name of SELEctbox options_from_collection_for_SELEct(@myrecords,"id","name"),# your options for this SELEct box :'data-remote' => 'true',# important for UJS :'data-url' => url_for(:controller => 'MyController',:action => 'getdata'),# we get the data from here! :'data-type' => 'json' # tell jQuery to parse the response as JSON! %> <%= SELEct_tag :second_SELEct,# name of SELEctbox "<option>Please SELEct something from first SELEct!</option>" %>
您的控制器:
class MyController < ApplicationController def getdata # this contains what has been SELEcted in the first SELEct box @data_from_SELEct1 = params[:first_SELEct] # we get the data for SELEctbox 2 @data_for_SELEct2 = Mymodel.where(:some_id => @data_from_SELEct1).all # render an array in JSON containing arrays like: # [[:id1,:name1],[:id2,:name2]] render :json => @data_for_SELEct2.map{|c| [c.id,c.name]} end end
在你的application.js中:
$(document).ready(function() { // #first_SELEct is the id of our first SELEct box,if the ajax request has been successful,// an ajax:success event is triggered. $('#first_SELEct').live('ajax:success',function(evt,data,status,xhr) { // get second SELEctbox by its id var SELEctbox2 = $('#second_SELEct'); // empty it SELEctbox2.empty(); // we got a JSON array in data,iterate through it $.each(data,function(index,value) { // append an option var opt = $('<option/>'); // value is an array: [:id,:name] opt.attr('value',value[0]); // set text opt.text(value[1]); // append to SELEct opt.appendTo(SELEctbox2); }); }); });
以上是大佬教程为你收集整理的ruby-on-rails – Ruby on Rails – 更改事件的下拉框全部内容,希望文章能够帮你解决ruby-on-rails – Ruby on Rails – 更改事件的下拉框所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。