如何通过javascript更新引导选择(selectpicker)标头

时间:2018-11-28 22:30:34

标签: javascript twitter-bootstrap bootstrap-select bootstrap-selectpicker

我想更改选择标头,但我做不到。

我尝试过:

$('select').selectpicker({header: "New Header"}).selectpicker('render');

和:

$('select').selectpicker({header: "New Header"}).selectpicker('refresh');

并且我已更新到最新版本。

1 个答案:

答案 0 :(得分:0)

不幸的是,仅在选择器初始化时才设置标题。不在refresh上,也没有在render上。因此,恐怕剩下的唯一选择就是通过更新DOM来设置标头:

$('select').selectpicker({ header: 'Header' });

function changeHeader() {
  $('.popover-title').contents().filter(function() {
    return this.nodeType === 3;
  })[0].nodeValue = 'New Header';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.3/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.3/js/bootstrap-select.min.js"></script>

<select>
  <option>Option 1</option>
  <option>Option 2</option>
  <option>Option 3</option>
</select>
<button type="button" onclick="changeHeader()">Change header</button>