简体   繁体   English

如果未通过单击选择,则不会显示“ jQuery选择”框

[英]jQuery Select box didn't show if no selected by click

I have two select boxes. 我有两个select框。 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. 当我从第二个框中的第一个select框中选择(单击)城市时,我会看到所选城市中的地区。 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? 有人可以帮忙吗?

Try this (See DEMO [in the end of js]) 试试这个(请参阅DEMO [js的结尾])

$('#get_car option:eq(2)').attr('selected', 'selected');
$('#get_car').trigger('change');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM