Reputation: 1002
I have two select
boxes. One with list of Cities and the second one with Districts of Cities. When I chose (by click) the City from the first select
box in the second box I see districts from selected City. That work perfectly, but when the City is selected automaticly by another script, the list of districts doesn't show - i have to manualy select the city from the list and then the list of districts appear.
Here is my script:
$.viewMap_get = {
'0' : $([]),
'Katowice' : $('#c1a, #c1b, #c6, #c7'),
'Kraków' : $('#c2a, #c2b, #c6, #c7'),
'Warszawa' : $('#c3a, #c3b, #c6, #c7'),
'Wrocław' : $('#c4a, #c4b, #c6, #c7'),
'Gdańsk' : $('#c5a, #c5b, #c6, #c7'),
};
$.each($.viewMap_get, function() { this.hide(); });
$('#get_city_district').hide();
$('#get_car').on('change', function() {
$('#return_city_district').show();
// hide all
$.each($.viewMap_get, function() { this.hide(); });
$('#get_city_district').hide();
// show current
$.viewMap_get[$(this).val()].show();
$('#get_city_district').show();
var id = $.viewMap_get[$(this).val()].attr("id");
$('#'+id).attr('selected', 'selected');
});
Could anyone help?
Upvotes: 0
Views: 174
Reputation: 30453
Try this (See DEMO [in the end of js])
$('#get_car option:eq(2)').attr('selected', 'selected');
$('#get_car').trigger('change');
Upvotes: 1