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

标签 javascript jquery-chosen

在 github 上,它说搜索框现在在所选选择框中是可选的。有谁知道怎么删除吗?

最佳答案

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>

关于javascript - 如何从 Chosen JS 中删除搜索框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12161354/

相关文章:

javascript - 如何获取 jQuery 选择的 drodpown 列表框的 onclick 事件?

javascript - 单页应用程序上的用户身份验证?

javascript - 使用 jQuery 获取 ListBox 中选定项目的数量

javascript - 使用 javascript 重置选定的选择框

html - 如何为选择的 Angular 添加 rtl 支持

jQuery Chosen div 落后于 Twitter Bootstrap Accordion

javascript - 使用 chosen.js,如何将图像添加到下拉项?

javascript - 创建孪生对象 a.k.a. 继承克隆

javascript - 查找变量 - Javascript

javascript - node.js如何用参数响应.js文件