无法更改Boostrap-select selectpicker的标题

5 html twitter-bootstrap bootstrap-select bootstrap-selectpicker

我想以编程方式更改我的Bootstrap选择selectpicker的标题。

我尝试了以下不起作用的JS代码。

$('#myPicker')
    .selectpicker({title: 'New title'})
    .selectpicker('render');
Run Code Online (Sandbox Code Playgroud)

还有其他方法可以做到这一点吗?

One*_*ion 6

仅当DOM已更新时,Selectpicker才会更新。下面的函数应该更新标题,因为它允许selectpicker附加一个新的html对象(DOM已更改)

function newTitle() {
    var selectpicker = $("#myPicker");
    selectpicker.selectpicker();
    selectpicker.selectpicker({title: 'New Title'}).selectpicker('render');
    html = '';
    selectpicker.html(html);
    selectpicker.selectpicker("refresh");
}
Run Code Online (Sandbox Code Playgroud)


小智 5

发布的代码似乎在代码段中有效。

$(document).ready(function() {
  $('#myPicker2')
    .selectpicker({
      title: 'New title'
    });
});
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.0/css/bootstrap-select.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.0/js/bootstrap-select.min.js"></script>

<select id="myPicker1" class="selectpicker" multiple title="Choose one of the following...">
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>

<select id="myPicker2" class="selectpicker" multiple title="Choose one of the following...">
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>
Run Code Online (Sandbox Code Playgroud)