如何在silviomoreto的Bootstrap-Select上添加选项值 [英] How to add option values on silviomoreto's Bootstrap-Select

查看:153
本文介绍了如何在silviomoreto的Bootstrap-Select上添加选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used silviomoreto's bootstrap-select but I'm having problems. I have 2 Dropdowns (Select) a Region dropdown and Cities. Now, I have a jquery onchange function, the function will change the values in the cities dropdown depending on the region selected. Here's my code:

<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/但我无法找到我的问题的相关答案。谢谢!

The problem is I think the compatibility? When I removed the class="selectpicker" it works fine, but I can't remove it because I used it for my dropdown ui. I checked the documentation in here https://developer.snapappointments.com/bootstrap-select/ but I can't find relevant answer to my question. Thanks!

推荐答案

尝试 selectpicker('refresh')


要以编程方式使用JavaScript更新选择,首先操作
select,然后使用refresh方法更新UI以匹配
新州。 删除或添加选项时必须,或者当
禁用/启用通过JavaScript 选择时。

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.

Snippet

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

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

这篇关于如何在silviomoreto的Bootstrap-Select上添加选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆