javascript - 如何在 silviomoreto 的 Bootstrap-Select 上添加选项值

标签 javascript jquery twitter-bootstrap

我使用了 silviomoreto 的 bootstrap-select,但遇到了问题。 我有 2 个下拉菜单(选择)一个区域下拉菜单和一个城市。现在,我有一个 jquery onchange 函数,该函数将根据所选区域更改城市下拉列表中的值。这是我的代码:

<select id="region" class="selectpicker"></select>
<select id="cities" class="selectpicker"></select>

$("#region").on('change', function () {
   $("#cities").html('<option>city1</option><option>city2</option>');
});

问题是我认为兼容性?当我删除 class="selectpicker"时,它工作正常,但我无法删除它,因为我将它用于我的下拉用户界面。我检查了这里的文档https://developer.snapappointments.com/bootstrap-select/但我找不到我的问题的相关答案。谢谢!

最佳答案

尝试selectpicker('refresh')

refresh()

To programmatically update a select with JavaScript, first manipulate the select, then use the refresh method to update the UI to match the new state. This is necessary when removing or adding options, or when disabling/enabling a select via JavaScript.

片段

$("#cities")
   .html('<option>city1</option><option>city2</option>')
   .selectpicker('refresh');

工作演示:http://jsfiddle.net/codeandcloud/nr54vx7b/

关于javascript - 如何在 silviomoreto 的 Bootstrap-Select 上添加选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23259617/

相关文章:

javascript - androidInterface 未定义。是什么赋予了?

html - 在 Bootstrap 模式中将文本区域宽度设置为 100%

html - Bootstrap 对齐按钮底部的 div

javascript - 将退格键按下事件传递给小程序并停止其传播

javascript - 如何在 jQuery 中显示移动耗时?

javascript - Dropzone - 自定义文本消息以与语言一起使用

jquery - AJAX 调用期间 Node.js 响应问题

html - 部分重叠CSS中的两个背景图像

javascript - 如何在父项悬停时淡化子项 div

javascript - 在 Angularjs 中渲染 HTML 和 CSS