动态加载数据时选择下拉列表的值

时间:2018-08-28 13:11:31

标签: jquery html jquery-select2

  • 我正在使用select2进行下拉

  • 当用户在输入字段中输入代码时。如果存在代码,则根据代码获取数据并使用ajax填充输入字段。

  • 问题是下拉列表中显示的国家。如果值匹配,则选择国家/地区,然后在下一个下拉列表中显示城市,因为该国家/地区没有选择

如果要在下拉列表中加载城市,我要选择城市

//country selected against the value return from ajax request

$("#country").val(data[0]['PA_COUNTRY']).trigger('change');
<select id="country" name="country">
  <option value="1" selected>UK</option>
</select>

//Problem is city show late against country value
// Thats why below line not select any city

$("#city").val(data[0]['CITY']).trigger('change');  
var state = data[0]['city'];

//When city will be show selected if i set 1 second delay to show city in dropdown

setTimeout(function(){

//this will work but this is not proper solution
$("#inst_cmb_province").val(state).trigger('change');
}, 2000);

我也使用它来选择下拉菜单,但这不起作用

   $("#city").select2();
   $('#city').trigger("liszt:updated");
   $("#city").val(data[0]['CITY']).prop("selected", true);
   $("#city").select2();
  $('#city').trigger("liszt:updated");

0 个答案:

没有答案
相关问题