首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从选择的JS中删除搜索框?

如何从选择的JS中删除搜索框?
EN

Stack Overflow用户
提问于 2012-08-28 22:19:38
回答 3查看 22.2K关注 0票数 22

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-03 09:42:42

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

代码语言:javascript
复制
$("#mySelect").chosen({
  "disable_search": true
});

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

代码语言:javascript
复制
$("#mySelect").chosen({
  "disable_search_threshold": 4
});

代码语言:javascript
复制
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'));
  });
});
代码语言:javascript
复制
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; }
代码语言:javascript
复制
<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>

票数 49
EN

Stack Overflow用户

发布于 2012-09-03 23:59:58

只需在需要时隐藏它

代码语言:javascript
复制
$('.chzn-search').hide();
票数 3
EN

Stack Overflow用户

发布于 2012-08-30 13:30:23

chosen.jquery.js只是用类chzn-searchdisplay:none中设置了搜索框的样式

代码语言:javascript
复制
<div class="chzn-search"><input type="text" autocomplete="off" style="display:none;" /></div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12161354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档