javascript - 在 mustache 模板中渲染 bootstrap-select 时出现问题

标签 javascript twitter-bootstrap-3 mustache

我在使用 bootstrap-select 时遇到问题。 当我在 html 文件中创建一个简单的选择时,一切都按预期工作:

<select class="selectpicker" multiple>
    <option selected>Mustard</option>
    <option selected>Ketchup</option>
    <option>Relish</option>
</select>

样式已从 bootstrap-select.min.css 正确应用

但是当我尝试在模板中渲染它时,它没有应用正确的 css/js。

<script type="template/text" id="template">
        <select class="selectpicker" multiple>
            <option selected>Mustard</option>
            <option selected>Ketchup</option>
            <option>Relish</option>
        </select>
        <table id="tblItems" class="table table-striped">
            <thead>
            <tr>
                <th>Id</th>
                <th>Action</th>
                ...
         </table>
</script>

在控制台中,我可以看到样式是从 bootstrap.min.css 应用的,而不是从 bootstrap-select.min.css 应用的

这就是我在头部定义的:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<script src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>

<link rel="stylesheet" href="css/bootstrap-select.min.css">

<script src="js/bootstrap-select.min.js"></script>

这是因为渲染模板时可能无法加载 boostrap-select 吗?

最佳答案

找到了!

我必须在模板呈现后初始化选择。

var template = $('#template').html();

var output = Mustache.render(template, {rows:context});

$("div").append(output);
$('.selectpicker').selectpicker();

关于javascript - 在 mustache 模板中渲染 bootstrap-select 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762640/

相关文章:

javascript - 使用 javascript 变量作为参数的 Flaskr url_for 对 python 不起作用

javascript - 将 withCredential 与 $resource 一起使用

html - 将 ul 固定在 Bootstrap 导航栏的右侧

javascript - Bootstrap 未在浏览器上应用

ruby-on-rails - Rails 3.1 模板处理程序

javascript - mustache JavaScript : Looping Through Arrays of Objects Output

javascript - 如何使用 axios 模拟虚假 url 进行单元测试?

javascript - jQuery-UI 可拖动和可放置的项目变为不可拖动

javascript - 按钮不适用于移动设备,但适用于 PC Bootstrap

javascript - 我如何在 UL 中显示 X 项并隐藏其余项?