简体   繁体   English

用于显示/隐藏div的动态搜索功能

[英]Dynamical search-function to show/hide divs

this is my first post on StackOverflow. 这是我在StackOverflow上的第一篇文章。 I hope it doesn't go horribly wrong. 我希望它不会出现可怕的错误。

<input type="Text" id="filterTextBox" placeholder="Filter by name"/>
<script type="text/javascript" src="/resources/events.js"></script>
<script>
$("#filterTextBox").on("keyup", function () {
    var search = this.value;
    $(".kurssikurssi").show().filter(function () {
        return $(".course", this).text().indexOf(search) < 0;
    }).hide();        
});

</script>

I have a javascript snippet like this on my school project, which can be found here: http://www.cc.puv.fi/~e1301192/projekti/tulos.html 我的学校项目中有一个这样的javascript片段,可以在这里找到: http//www.cc.puv.fi/~e1301192/projekti/tulos.html

So the search bar at the bottom is supposed to filter divs and display only those, that contain certain keyword. 因此底部的搜索栏应该过滤div并仅显示包含特定关键字的那些。 (t.ex, if you type Digital Electronics, it will display only Divs that contain text "Digital Electronics II" and "Digital Electronics". Right now, if I type random gibberish, it hides everything like it's supposed to, but when I type in the beginning of a course name, it will not hide the courses that dont contain the certain text-string. (t.ex,如果你键入数字电子,它将只显示包含文本“数字电子II”和“数字电子”的Div。现在,如果我输入随机乱码,它隐藏了一切,就像它应该的那样,但当我键入课程名称的开头,它不会隐藏不包含特定文本字符串的课程。

Here is an example that I used (which works fine): http://jsfiddle.net/Da4mX/ 这是我使用的一个例子(工作正常): http//jsfiddle.net/Da4mX/

Hard to explain, but I hope you realize if you try the search-function on my page. 很难解释,但我希望你能意识到,如果你在我的页面上尝试搜索功能。 Also, I'm pretty new to javascript, and I get the part where you set the searchbox's string as var search, the rest I'm not so sure about. 此外,我是javascript的新手,我得到了将搜索框的字符串设置为var搜索的部分,其余的我不太确定。

Please help me break down the script, and possibly point where I'm going wrong, and how to overcome the problem. 请帮我分解脚本,并指出我出错的地方,以及如何克服这个问题。

in your case I think you show and hide the parent of courses so you can try 在你的情况下,我认为你显示和隐藏课程的父母,所以你可以尝试

$("#filterTextBox").on("keyup", function () {
    var search = $(this).val().trim().toLowerCase();
    $(".course").show().filter(function () {
        return $(this).text().toLowerCase().indexOf(search) < 0;
    }).hide();        
});

Try this this is working now, paste this code in console and check, by searching. 试试这个现在正在运行,将此代码粘贴到控制台中并通过搜索进行检查。

 $("#filterTextBox").on("keyup", function () { var search = this.value; if( search == '') { return } $( ".course" ).each(function() { a = this; if (a.innerText.search(search) > 0 ) {this.hidden = false} else {this.hidden = true} }); }) 

Check and the search is now working. 检查并且搜索现在正在运行。

Your problem is there : 你的问题在那里:

return $(".course", this)

From jquery doc: http://api.jquery.com/jQuery/#jQuery-selection 来自jquery doc: http//api.jquery.com/jQuery/#jQuery-selection

Internally, selector context is implemented with the .find() method, so $( "span", this ) is equivalent to $( this ).find( "span" ) 在内部,选择器上下文是使用.find()方法实现的,所以$(“span”,this)相当于$(this).find(“span”)

filter function already check each elements then, when you try to put $(".course") in context, it will fetch all again... filter函数已经检查了每个元素,当你尝试将$(“。course”)放在上下文中时,它将再次获取所有元素...

Valid code : 有效代码:

$("#filterTextBox").on('keyup', function()
{
    var search = $(this).val().toLowerCase();
    $(".course").show().filter(function()
    {
        return $(this).text().toLowerCase().indexOf(search) < 0;
    }).hide();
});

In fact, you can alternatively use :contains() CSS selector, but, it is not optimized for a large list and not crossbrowser 实际上,您也可以使用:contains()CSS选择器,但是,它没有针对大型列表而不是针对crossbrowser进行优化

http://caniuse.com/#search=contains http://caniuse.com/#search=contains

You were accessing the wrong elements. 你正在访问错误的元素。 This should be working: 这应该是有效的:

$(".kurssikurssi").find('.course').show().filter(function () {
      var $this = $(this)
      if($this.text().indexOf(search) < 0){
              $this.hide()
      }
 })

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

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