0

嗨,我正在使用 jquery ui 自动完成组合框插件,我最初是在document.ready

jQuery('#combolist_city').combobox();

我在页面加载时设置了一些选项

<select id="combolist_city" class="city" name="search[city]">
                            <option value="0">Select city</option>
                            <?php 
                                if(isset($city_list))
                                {
                                    foreach($city_list as $city_data)
                                    {
                                        if(isset($selected_city) && ($selected_city == $city_data['CityID']))
                                        {
                                            echo "<option selected='selecetd' value=".$city_data['CityID'].">".$city_data['CityName']."</option>";
                                        }
                                        else 
                                        {
                                            echo "<option value=".$city_data['CityID'].">".$city_data['CityName']."</option>";  
                                        }
                                    }   
                                }
                            ?>                      
                        </select>

现在我想改变他的选择,我想通过

jQuery("#combolist_city").combobox({
                                    initialValues: ['aaa','bbb','ccc']
                                });  

但它不起作用,它没有重新创建选项,

我该怎么做,请帮助.................................

4

1 回答 1

1

您必须手动完成。首先销毁组合框并清空选择。追加新选项并再次构建组合框:

var aList = {'id1': 'val1', 'id2': 'val2', 'id3': 'val3'};
var sKey;
$("#combolist_city").combobox('destroy').empty();
for (sKey in aList) {
    $("#combolist_city").append('<option value="' + sKey + '">' + aList[sKey] + '</option>');
}
$("#combolist_city").combobox();

另请参阅此示例

于 2012-07-10T06:11:00.627 回答