动态添加搜索框

有时候我们做的项目 需要做搜索,有时候复杂的搜索需要多个搜索框。如下:

有时候我们需要动态添加,这时候我们不需要重复添加代码,只需要用jquery的clone方法赋值对应的对话框即可。

html:

 1 <div  class="container"  style="margin-bottom: 15px">
 2 
 3 <div style="margin-bottom: 15px">
 4 <select class="selectpicker" data-live-search="true" style="top: 200px">
 5   <option data-tokens="ketchup mustard">SN</option>
 6   <option data-tokens="mustard">IP</option>
 7   <option data-tokens="frosting">园区</option>
 8   <option data-tokens="frosting">库房名称</option>
 9 </select>
10  <select class="selectpicker" data-live-search="true" style="top: 200px">
11   <option data-tokens="ketchup mustard">精确搜索</option>
12   <option data-tokens="mustard">模糊搜索</option>
13 
14 </select>
15 
16 <input type="text" class="form-control" placeholder="多个IP和sn请空格隔开。">
17 <button class="btn btn-default add" onclick="Add_Row(this)"><i class="fa fa-plus" aria-hidden="true"></i></button>
18 <button class="btn btn-default del" onclick="Del_Row(this)" ><i class="fa fa-minus" aria-hidden="true"></i></button>
19 <button class="btn btn-primary  ser"><i class="fa fa-search" aria-hidden="true"></i> 搜索</button>
20 </div>
21 
22 </div>

 js部分:

 1 <script>
 2     function Add_Row(ths) {
 3         var cl_p=$(ths).parent().clone();
 4         $(ths).parent().parent().append(cl_p);
 5 
 6         $(ths).parent().find('.ser').remove();
 7         $(ths).parent().find('.add').remove();
 8     }
 9     function Del_Row(ths) {
10         var tag=$(ths).parent().prev();
11         if (tag.length===0){
12             $(ths).parent().remove();
13         }else {
14             tag.remove();
15         }
16     }
17 </script>

 导入css和js

 1      <!--<link href="bootstrap-select.css" rel="stylesheet">-->
 2      <link href="bootstrap.min.css" rel="stylesheet">
 3     <!--<script src="bootstrap-select.min.js"></script>-->
 4     <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/i18n/defaults-*.min.js"></script>-->
 5 <!-- Latest compiled and minified CSS -->
 6 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">
 7 <link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">
 8 <script src="jquery-2.1.1.min.js"></script>
 9 <script src="bootstrap.min.js"></script>
10 <!-- Latest compiled and minified JavaScript -->
11 <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>

 按照上面的方法实现了动态加载搜索框,但是出现新的问题,就是select下拉框失效了。下拉框点击无效。

查询网上解决方法,需要重新初始化selecter。可以生效。但是新的问题又出现。

在初始化 的时候。又出现一个问题,在含有bootstrap-select的div里 有嵌套个bootstrap-select。如下:

解决方法:

js:

cl_p.find('.bootstrap-select').replaceWith(function() { return $('select', this); });

 

 问题解决地址:

https://github.com/silviomoreto/bootstrap-select/issues/605

 

posted @ 2017-08-16 13:24  evil_liu  阅读(1084)  评论(1)    收藏  举报