使用Bootstrap在选择选项中添加搜索功能

o0lyfsai  于 2023-08-01  发布在  Bootstrap
关注(0)|答案(6)|浏览(144)

我一直在尝试使用bootstrap在select表单中添加搜索功能。这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <meta name="description" content="">
  <meta name="author" content="">
  <link rel="icon" href="http://getbootstrap.com/favicon.ico">

  <title>Depots list</title>

  <!-- Bootstrap core CSS -->
  <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">

  <!-- Latest compiled and minified bootstrap-select CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">

  <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  <link href="http://getbootstrap.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

  <!-- Custom styles for this template -->
  <link href="http://getbootstrap.com/examples/starter-template/starter-template.css" rel="stylesheet">
  <link href="./css/st.css" rel="stylesheet">

 <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
 <!--[if lt IE 9]><script src="http://getbootstrap.com/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
 <script src="http://getbootstrap.com/assets/js/ie-emulation-modes-warning.js"></script>

 <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
 <!--[if lt IE 9]>
 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
 <![endif]-->
 </head>


<body>

<div class="container">

  <div class="row">
    <div class="col-xs-12">
      <div class="box">
        <!-- /.box-header -->
        <div class="box-body">
                  <form>
                   <div class="form-group row">
                   <label for="" class="col-sm-2 form-control-label">Country</label>
                  <div class="col-sm-10">
                    <select class="form-control selectpicker" id="select-country" placeholder="" data-live-search="true">
                        <options>China</options>
                        <options>Malaysia</options>
                        <options>Singapore</options>
                    </select>
                  </div>
                </div>
              </form>
        </div><!-- /.box-body -->
      </div><!-- /.box -->
    </div><!-- /.col -->
  </div><!-- /.row -->
</div><!-- /.container -->

字符串
select表单中的form-controlselectpicker两个类矛盾。我的代码只对form-control类有效。
对于选择搜索,我使用了以下方法:https://developer.snapappointments.com/bootstrap-select/examples/#live-search
我的问题是:如何在select标签中同时使用form-controlselectpicker类?我已经用正常的程序写了一个空格的类名。但似乎不起作用。

uplii1fm

uplii1fm1#

要获得搜索功能,必须为每个选项设置data-tokens属性。这是添加后的代码。如果你需要额外的帮助,请告诉我。

$(function() {
  $('.selectpicker').selectpicker();
});

个字符

jc3wubiy

jc3wubiy2#

多项选择表格和可搜索

$(function() {
	$('.selectpicker').selectpicker();
});

个字符

kqlmhetl

kqlmhetl3#

<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.min.css" />

<select class="selectpicker" data-show-subtext="true" data-live-search="true">
<option data-tokens="name">name</option>
<option data-tokens="family">family</option>
</select>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.min.js"></script>

字符串

zed5wv10

zed5wv104#

#Search Box in Select Option in Asp.net Core#
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/css/bootstrap-select.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/js/bootstrap-select.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

-At View Page
 <select id="empselect" class="form-control selectpicker" data-live-search="true" asp-for="EmpId" asp-items="@(new SelectList(@ViewBag.empInilistvb,"EmpId", "EmpInitial"))"></select>

字符串
我认为data-token=不是强制属性。
我在Asp.net Core中使用了Select Option中的Search Box。

tuwxkamq

tuwxkamq5#

如果您使用的是bootstrap selectpicker,只需在select标记中添加一个HTML属性,并在脚本中对其进行初始化。
data-live-search=“true”

<script>
    $(document).ready(function(){
        $('.classname').selectpicker();
    }); 
</script>

字符串

zz2j4svz

zz2j4svz6#

我用过这个非常简单的JS,它能创造奇迹

<html>
  <head>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.min.css" />
  </head>

  <div class="container">
    <div class="row">
      <h2>Bootstrap-select example</h2>
      <p>This uses <a href="https://silviomoreto.github.io/bootstrap-select/">https://silviomoreto.github.io/bootstrap-select/</a></p>
      <hr />
    </div>

    <div class="row-fluid">
      <select class="selectpicker" data-show-subtext="true" data-live-search="true">
        <option data-subtext="Rep California">Tom Foolery</option>
        <option data-subtext="Sen California">Bill Gordon</option>
        <option data-subtext="Sen Massacusetts">Elizabeth Warren</option>
        <option data-subtext="Rep Alabama">Mario Flores</option>
        <option data-subtext="Rep Alaska">Don Young</option>
        <option data-subtext="Rep California" disabled="disabled">Marvin Martinez</option>
      </select>
      <span class="help-inline">With <code>data-show-subtext="true" data-live-search="true"</code>. Try searching for california</span>
    </div>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.min.js"></script>
</html>

字符串
使用https://silviomoreto.github.io/bootstrap-select/

相关问题