Chrome选择列表样式行为过滤选择列表选项(Chrome Select List Style Behavior Filtering Select List Options)

编程入门 行业动态 更新时间:2024-10-26 05:17:28
Chrome选择列表样式行为过滤选择列表选项(Chrome Select List Style Behavior Filtering Select List Options)

在Chrome中,这些行为不符合预期,如果使用size属性,则样式选项不起作用。 我怎样才能做到这一点?

<select size=2> <option>1</option> <option selected>2</option> <option style="display: none;">3</option> <option>4</option> </select> <br/> <select> <option>1</option> <option selected>2</option> <option style="display: none;">3</option> <option>4</option> </select>

http://jsfiddle.net/D9X9U/1/

我在这里的最终游戏是有一个搜索框,可以过滤某种选择列表。

In Chrome these do not behave as expected, the style options doesn't work if a size attribute is used. How can I make this work?

<select size=2> <option>1</option> <option selected>2</option> <option style="display: none;">3</option> <option>4</option> </select> <br/> <select> <option>1</option> <option selected>2</option> <option style="display: none;">3</option> <option>4</option> </select>

http://jsfiddle.net/D9X9U/1/

My end game here is to have a search box that filters a select list of some kind.

最满意答案

我最终使用隐藏的选择列表并在两者之间移动元素。 如果有人发现了这个并且可以改善这一点,那么我会将你的标记作为答案。 我或多或少地通过javascript和jquery摸索我的方式,所以我确信可以做很多事情来改善它。

http://jsfiddle.net/x6cfF/1/

编辑:任何发现这个寻找解决方案的人,这里有一个更好的解决方案https://codereview.stackexchange.com/questions/23706/better-way-to-filter-select-list/23710?noredirect=1#23710

<input type="search" id="SearchBox" /> <br /> <div class="scrollable" id="CustomerSelectDiv"> <select size=2 class="scrollableinside" id="CustomerSelect"> <option value=100>test</option> <option value=101>test1</option> <option value=102>test2</option> <option value=103>test3</option> </select> </div> <div style="display: none;"> <select id="CustomerSelectHidden"></select> </div> <script type="text/javascript"> window.onload=function() { var $options = $('#CustomerSelect option'); document.getElementById("SearchBox").onkeyup = function () { var $HiddenOptions = $('#CustomerSelectHidden option'); $HiddenOptions.each(function (index, value) { document.getElementById('CustomerSelect').appendChild(this); }); var search = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); var element = $options.filter(function () { var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); return !~text.indexOf(search); }).appendTo(document.getElementById('CustomerSelectHidden')); } } </script>

I ended up using a hidden select list and moving elements between the two. If someone finds this and can improve this please do so, I will mark yours as the answer. I'm more or less fumbling my way through javascript and jquery so I'm sure a lot can be done to make this better.

http://jsfiddle.net/x6cfF/1/

Edit: anyone who finds this looking for a solution, there is a better one here https://codereview.stackexchange.com/questions/23706/better-way-to-filter-select-list/23710?noredirect=1#23710

<input type="search" id="SearchBox" /> <br /> <div class="scrollable" id="CustomerSelectDiv"> <select size=2 class="scrollableinside" id="CustomerSelect"> <option value=100>test</option> <option value=101>test1</option> <option value=102>test2</option> <option value=103>test3</option> </select> </div> <div style="display: none;"> <select id="CustomerSelectHidden"></select> </div> <script type="text/javascript"> window.onload=function() { var $options = $('#CustomerSelect option'); document.getElementById("SearchBox").onkeyup = function () { var $HiddenOptions = $('#CustomerSelectHidden option'); $HiddenOptions.each(function (index, value) { document.getElementById('CustomerSelect').appendChild(this); }); var search = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); var element = $options.filter(function () { var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); return !~text.indexOf(search); }).appendTo(document.getElementById('CustomerSelectHidden')); } } </script>

更多推荐

本文发布于:2023-07-30 07:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   样式   选项   Select   Chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!