如何从Chosen JS中删除搜索框?

时间:2012-08-28 14:19:38

标签: javascript jquery-chosen

在github上,它表示搜索框现在在所选的选择框中是可选的。有谁知道如何删除它?

3 个答案:

答案 0 :(得分:45)

当前版本的Chosen提供了两种控制搜索框显示的方法。两者都在初始化期间作为选项传入。要隐藏搜索框,请完全传递选项"disable_search": true

$("#mySelect").chosen({
  "disable_search": true
});

或者,如果您想要显示搜索,如果有一定数量的选项,请使用选项"disable_search_threshold": numberOfOptions(其中numberOfOptions是显示搜索框之前所需的最少选项数):

$("#mySelect").chosen({
  "disable_search_threshold": 4
});

jQuery(function($) {
  // Create a set of OPTION elements from some dummy data
  var words = ["lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit", "duis", "ullamcorper", "diam", "sed", "lorem", "mattis", "tristique", "integer", "pharetra", "sed", "tortor"],
      options = $($.map(words, function(word) {
        return $(document.createElement('option')).text(word)[0];
      }));
  $('select').each(function() {
    // Add the dummy OPTIONs to the SELECT
    var select = $(this).append(options.clone());
    // Initialize Chosen, using the options from the
    // `data-chosen-options` attribute
    select.chosen(select.data('chosen-options'));
  });
});
body {
  font-family: sans-serif;
  font-size: .8em; }
label {
  display: block;
  margin-bottom: 1.4em; }
  label .label {
    font-weight: bold;
    margin-bottom: .2em; }
select {
  width: 14em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.jquery.min.js"></script>

<label>
  <div class='label'>Default behavior</div>
  <select name='default' data-chosen-options='{}'></select>
</label>
<label>
  <div class='label'>No search at all</div>
  <select name='no-search' data-chosen-options='{ "disable_search": true }'></select>
</label>
<label>
  <div class='label'>Search iff more than 4 items</div>
  <select name='conditional-search' data-chosen-options='{ "disable_search_threshold": 4 }'></select>
</label>
<label>
  <div class='label'>Search iff more than 32 items</div>
  <select name='conditional-search' data-chosen-options='{ "disable_search_threshold": 32 }'></select>
</label>

答案 1 :(得分:2)

只需在需要时隐藏

$('.chzn-search').hide();

答案 2 :(得分:0)

selected.jquery.js只是在div中设置了搜索框display:none的样式,其中包含类chzn-search

<div class="chzn-search"><input type="text" autocomplete="off" style="display:none;" /></div>