有三个国家,州和城市的下拉框。
.entry.float
%label{:for => "hospital-country"} Country
= select_tag :country_id, options_for_select(["Select option"] + @countries.collect{ |c| [c.name, c.id] },@hospital.country_id),{:name =>"hospital[country_id]",:class => "select r5",:id => "hospital-country",:onchange=>"country_change()"}
.entry.float
%label{:for => "hospital-state"} State
= select_tag :state, options_for_select(["Select option"]),{:name =>"hospital[state]",:id => 'hospital-state',:class => "select r5",:disabled => true,:onchange=>"state_change()"}
.entry.float
%label{:for => "hospital-city"} City
= select_tag :city, options_for_select(["Select option"]),{:name =>"hospital[city]",:id => 'hospital-city',:class => "select r5",:disabled => true}
在哪个国家/地区填充了
@countries = WorldCountry.all
` 州和城市分别使用jquery
填充国家和州下拉框的ajax调用function country_change() {
$('#hospital-state').find('option[value!="Select option"]').remove();
$('#hospital-state').next().text("Select option");
$('#hospital-state').attr('disabled', 'disabled');
$('#hospital-city').find('option[value!="Select option"]').remove();
$('#hospital-city').next().text("Select option");
$('#hospital-city').attr('disabled', 'disabled');
if ($('#hospital-country').val() != "" && $('#hospital-country').val() != "Select option") {
$.ajax({
type : 'post',
url : '/hospitals/country_change',
data : {
country_id : $('#hospital-country').val()
},
dataType : "json",
success : function(data) {
var select = $('#hospital-state');
if (data.length > 0) {
select.removeAttr('disabled');
$.each(data, function(key, value) {
$("<option/>").val(value[1]).text(value[0]).appendTo(select);
});
}
},
failure : function() {
}
})
}
};
这一切都有效。但是当验证失败时,我只能保留国家的价值。 如何保留仅在ajax中有效的州和城市的价值。
答案 0 :(得分:0)
将视图更改为
.entry.float
%label{:for => "hospital-state"} State
-if !@states.blank?
= select_tag :state, options_for_select(["Select option"] + @states.collect{ |s| [s.name, s.id] },@selected_state),{:name =>"hospital[state]",:class => "select r5",:id => "hospital-state",:onchange=>"state_change()"}
- else
= select_tag :state, options_for_select(["Select option"]),{:name =>"hospital[state]",:id => 'hospital-state',:class => "select r5",:onchange=>"state_change()"}
.entry.float
%label{:for => "hospital-city"} City
- if !@cities.blank?
= select_tag :city, options_for_select(["Select option"] + @cities.collect{ |c| [c.name, c.id] },@selected_city),{:name =>"hospital[city]",:class => "select r5",:id => "hospital-state"}
-else
= select_tag :city, options_for_select(["Select option"]),{:name =>"hospital[city]",:id => 'hospital-city',:class => "select r5",:disabled => true}
在控制器中 如果验证失败
@states =WorldRegion.where("world_country_id=?",params[:hospital][:country_id])
@selected_state = WorldRegion.find_by_id(params[:hospital][:state]).id rescue nil
@cities = WorldCity.where("world_country_id=? and world_region_id=?",params[:hospital][:country_id],params[:hospital][:state])
@selected_city = WorldCity.find_by_id(params[:hospital][:city]).id rescue nil
render action: "new"
工作正常。 谢谢 哈里