$(function(){ $("#area_parentID").change(function(){ $("#next_select").html("<select name='streat' id='streat'></select>"); $.post( "/banks/select_with_ajax", {"parentID":$("#area_parentID").val()}, function(data){ var datas = eval(data); //alert(datas); $.each(datas,function(i){ $("<option value='"+datas[i].area.id+"'>"+datas[i].area.name+"</option>").appendTo($("#streat")); }); }); }); });
<tr>
<td>地域</td><td><%=
@areas = Area.find(:all, :select => 'name,id',:conditions=>"parentID=0").collect{|t| [t.name,t.id]}.insert(0,["请选择",0])
select("area","parentID",@areas,{},{:selected=>1})%><span id='next_select'><select name="streat" id="streat"></select></span></td>
</tr>
def select_with_ajax
@areas = Area.find(:all, :select => "name,id",:conditions => ["parentID = ?", params[:parentID]])
render(:json => @areas.to_json,:layout => false)
end
刚弄的rails+jquery的二级联动,记性不好,在这记下来