Bootstrap-Select - 如何保持静态标题?

时间:2017-02-12 23:50:33

标签: jquery twitter-bootstrap html-select bootstrap-select bootstrap-selectpicker

简单的问题。我的选择下拉列表是这样的:

<select class="selectpicker" multiple data-container="body" data-title="Options" data-size="10" data-width="100%" data-selectedTextFormat="static" >  
    <option value="default">Test</option>
</select>

无论选择什么,我都希望保留该标题(&#34;选项&#34;)。即使我这样做:

$('.selectpicker').selectpicker('val', "default");

我希望它能保留标题&#34;选项&#34;。正如您所看到的,我尝试了data-titledata-selectedTextFormat属性,但它没有用。

2 个答案:

答案 0 :(得分:1)

请参阅Options

  

选项可以通过数据属性或JavaScript传递。对于数据   属性,将选项名称附加到data-,如data-style=""或   data-selected-text-format="count"

您的数据属性不正确,应为data-selected-text-format="static"

使用数据属性

&#13;
&#13;
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">

<div class="container">
  <select class="selectpicker" multiple data-title="Options" data-size="10" data-width="100%" data-selected-text-format="static">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
  </select>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
&#13;
&#13;
&#13;

使用JavaScript

&#13;
&#13;
$('.selectpicker').selectpicker({
  selectedTextFormat: 'static',
  width: '100%',
  size: 10,
  title: 'Options'
});
&#13;
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">

<div class="container">
  <select class="selectpicker" multiple>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
  </select>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您希望无论用户选择什么,默认值始终保持不变,那么您可以将每个<option>的值设置为相同并更改标题

 <select class="selectpicker" multiple data-container="body" data-title="Options" data-size="10" data-width="100%" data-selectedTextFormat="static" >  
     <option value="Options">Title1</option>
     <option value="Options">Title2</option>
     <option value="Options">Title3</option>
     <option value="Options">Title4</option>
     <option value="Options">Title5</option>
     <option value="Options">Title6</option>
 </select>

但我不知道为什么你会这么想。