【发布时间】:2013-12-22 11:51:33
【问题描述】:
我使用引导程序multi-select,我想使用 ajax 更新流选项
在初始化时填充我的多选
<select name="model" class="multiselect" multiple="multiple">
<? foreach ($sel_models as $mod) { ?>
<option value="<?= $mod ?>" <?= ($mod == $params['model']) ? 'selected' : '' ?>><?= $mod ?></option>
<? } ?>
</select>
然后在事件中我想用以下 ajax 更新我的选项列表
我尝试使用重建方法,但创建后不会触发下拉菜单
$.ajax({
type: 'post',
url: "helper/ajax_search.php",
data: {models: decodeURIComponent(brands)},
dataType: 'json',
success: function(data) {
$('select.multiselect').empty();
$('select.multiselect').append(
$('<option></option>')
.text('alle')
.val('alle')
);
$.each(data, function(index, html) {
$('select.multiselect').append(
$('<option></option>')
.text(html.name)
.val(html.name)
);
});
$('.multiselect').multiselect('rebuild')
},
error: function(error) {
console.log("Error:");
console.log(error);
}
});
使用 firebug 我可以看到列表已生成,但在选择时不会显示
【问题讨论】:
-
如果您刚刚使用 ajax 更改了选择框选项,则需要调用 $('.multiselect').multiselect('rebuild') 。它将使用选择框中的选定值再次重建。
标签: javascript jquery ajax twitter-bootstrap