页面代码如下:
省份列表框:
市列表框:
js:
action中代码如下:
省份列表框:
<%=@resources=Province.find :all ,:conditions=>"resource_level=1"
@provices<<Province.new(:id=>0,:resource_name=>"--请选择省份--")
collection_select(:move,"id",@resources,:id,:resource_name,{},
:onchange =>"change('move_id','move_city_id')")
%>
市列表框:
<select id="move_city_id" name="city[city_id]">
<option value="" selected="selected">--请选择市--</option>
</select>
js:
//ajax联动,需要传递两个列表框id
function change(levelone_id,leveltwo_id)
{
new Ajax.Request(
'/manage/ajax_find_leveltwo/'+$(levelone_id).value,
{
method: 'get',
requestHeaders:{Accept:'application/json'},
onComplete: function showResponse(transport)
{
var json = eval(transport.responseText);
$(leveltwo_id).options.length=1;
for(var i=0;i<json.length;i++)
{
var oNewNode=new Option();
oNewNode.value=json[i].city.id;
oNewNode.text=json[i].resource.resource_name;
$(leveltwo_id).options.add(oNewNode);
}
},
asynchronous:true,
evalScripts:true
});
}
action中代码如下:
def ajax_find_leveltwo
@citys=City.find :all,:conditions=>"parent_id=#{params[:id]} "
render :text => @citys.to_json
end