简体   繁体   English

多选搜索

[英]Search in a multi-select

I want to create a search for a multi-select but i have no idea where to start. 我想为多选创建搜索,但是我不知道从哪里开始。

I'd want the search to remove all the other options and only show the one that matches the search(the search would go through each word(first name, last name, and also username) excepting email. 我希望搜索删除所有其他选项,只显示与搜索匹配的选项(搜索将遍历每个单词(名字,姓氏和用户名),但电子邮件除外。

I'd create the event listener on the keyup but after that I literally have no idea on how to continue. 我会在键盘上创建事件侦听器,但是在那之后我真的不知道如何继续。

This is how my select looks like: 这是我的选择的样子:

 $("#addselect_searchtext").keyup(function() { alert("do_search?"); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="span5"> <label for="addselect">Users</label> <div class="userselector" id="addselect_wrapper"> <select name="addselect" id="addselect" size="20" class="form-control" style=""> <optgroup id="toexec" label="All Students (2)"> <option value="2">Admin User (admin, andrei@yahoo.com)</option> <option value="3">Student Test (student, studenttest@mailinator.com</option> </optgroup> </select> <div class="form-inline"> <label for="addselect_searchtext">Search</label><input type="text" name="addselect_searchtext" id="addselect_searchtext" size="15" value="" class="form-control"><input type="button" value="Clear" class="btn btn-secondary mx-1" id="addselect_clearbutton" disabled=""></div> </div> </div> 

You can do something like this, It will show those options that contains the value of your input : 您可以执行以下操作,它将显示包含input值的那些options

$("#addselect_searchtext").keyup(function() {
  var thisVal = $(this).val().toLowerCase();
  $('#addselect option').css("display",function() {
    return $(this).text().toLowerCase().indexOf(thisVal) > -1 ? "block" : "none";
  });
});

Working demo 工作演示

 $("#addselect_searchtext").keyup(function() { var thisVal = $(this).val().toLowerCase(); $('#addselect option').css("display",function() { return $(this).text().toLowerCase().indexOf(thisVal) > -1 ? "block" : "none"; }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="span5"> <label for="addselect">Users</label> <div class="userselector" id="addselect_wrapper"> <select name="addselect" id="addselect" size="20" class="form-control" style=""> <optgroup id="toexec" label="All Students (2)"> <option value="2">Admin User (admin, andrei@yahoo.com)</option> <option value="3">Student Test (student, studenttest@mailinator.com</option> </optgroup> </select> <div class="form-inline"> <label for="addselect_searchtext">Search</label><input type="text" name="addselect_searchtext" id="addselect_searchtext" size="15" value="" class="form-control"><input type="button" value="Clear" class="btn btn-secondary mx-1" id="addselect_clearbutton" disabled=""></div> </div> </div> 

Like this? 像这样?

Tested in Chrome and Edge - it does not work in IE11 经过Chrome和Edge测试-在IE11中不起作用

I added a workaround - it is a bit hard to test since stacksnippets don't run in IE so I have made a fiddle 我添加了一种解决方法-由于stacksnippets无法在IE中运行,因此测试起来有点困难,所以我做了一个小提琴

For some reason the disable/enable does not show until you mouse over the select in IE11 由于某些原因,直到将鼠标悬停在IE11中的选择上时,禁用/启用才会显示

 $("#addselect_searchtext").on("input",function() { var val = this.value.toLowerCase() $("#toexec option").each(function() { var show = $(this).text().toLowerCase().indexOf(val) !=-1; $(this).toggle(show) $(this).prop("disabled",!show) }) $("#addselect_clearbutton").prop("disabled",this.value=="") }); $("#addselect_clearbutton").on("click",function() { $("#addselect_searchtext").val("").trigger("input") }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="span5"> <label for="addselect">Users</label> <div class="userselector" id="addselect_wrapper"> <select name="addselect" id="addselect" size="5" class="form-control" style=""> <optgroup id="toexec" label="All Students (2)"> <option value="2">Admin User (admin, andrei@yahoo.com)</option> <option value="3">Student Test (student, studenttest@mailinator.com</option> </optgroup> </select> <div class="form-inline"> <label for="addselect_searchtext">Search</label><input type="text" name="addselect_searchtext" id="addselect_searchtext" size="15" value="" class="form-control"><input type="button" value="Clear" class="btn btn-secondary mx-1" id="addselect_clearbutton" disabled=""></div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM