实施搜索栏以过滤列表项

时间:2016-09-21 10:59:44

标签: javascript jquery css

我有一个项目列表,我希望人们可以使用搜索栏进行过滤。该列表当前分为6个类别,当您单击类别标题时,将显示其内容。

这些类别的内容我希望用户能够搜索。

我添加了搜索栏,它会在您键入时自动过滤,但它不起作用。



$(document).ready(function() {
	var tcc = new $.ToClickControl($('.infoContainer'));

$('#box').keyup(function(){
	var valThis = $(this).val().toLowerCase();
		if(valThis == ""){
			$('.three-quarters-column > li').show();
		} else {
			$('.three-quarters-column > li').each(function(){
			var text = $(this).text().toLowerCase();
			(text.indexOf(valThis) >= 0) ? $(this).show() : $(this).hide();
		});
	};
});

});

;(function($) {

    $.ToClickControl = function(el, options) {

        var defaults = {
            activeClass: 'activeOption',

            onCallback: function() {}
        };

        var plugin = this;

        plugin.settings = {};

        var init = function() {
            plugin.settings = $.extend({}, defaults, options);
            plugin.el = el;

            active();
            bindEvents();
        };

        plugin.Destroy = function() {
            unbindEvents();
            plugin.el.removeData();
        };

        var bindEvents = function() {
            var select_container = plugin.el.find('.selectOptions');
            var matching_container = plugin.el.find('.matchingOptions');
            var activeClass = plugin.settings.activeClass;

            select_container.find('p').on('click', function() {
                if( $(this).attr('title') && !$(this).hasClass(activeClass) ) {

                    plugin.el.find('.' + activeClass).removeClass(activeClass);
                    $(this).addClass(activeClass);

                    matching_container.find('div[title]').hide('fast');
                    findMatch(matching_container, $(this).attr('title') ).show('fast');

                }
            });
        };

        var unbindEvents = function() {

        };

        var findMatch = function( container, title ) {
            return container.children('div[title="' + title + '"]');
        };

        var active = function() {
            var select_container = plugin.el.find('.selectOptions');
            var matching_container = plugin.el.find('.matchingOptions');
            var activeClass = plugin.settings.activeClass;

            if( plugin.el.find('.' + activeClass) )
                findMatch(matching_container, select_container.find('.' + activeClass).attr('title') ).show();
        };

        init();

    };

})(jQuery);

.subMainContent {
	box-sizing:border-box;
	padding:80px;
	font-size: 1.2em;
	line-height:30px;
}
.subMainContent h1 {
	font-family: 'Oswald';
}
.subMainContent img {
	width:100%;
	height:auto;
	margin-bottom:40px;
}
.subMainContent .leftColumn {
	width: 50%;
	box-sizing:border-box;
	float:left;
	padding-right: 40px;
}
.subMainContent .rightColumn {
	width: 49%;
	padding-left: 40px;
	float:left;
	box-sizing:border-box;
}
.subMainContent .rightColumn ul {
	box-sizing:border-box;
	padding-left: 30px;
}

@media all and (max-width:768px) {
	.subMainContent {
		padding:20px;
	}
	.subMainContent .leftColumn {
		width:100%;
		padding-right: 0px;
	}
	.subMainContent .rightColumn {
		width:100%;
		padding-left: 0px;
	}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<input placeholder="Search" id="box" type="text" />
<div class="infoContainer">
<div class="quarter-column selectOptions">


<p class="activeOption" title="1">Centre for American Studies</p>

<p title="2">School of Architecture</p>

<p title="3">School of Arts</p>

<p title="4">School of English</p>

<p title="5">School of European Culture and Language</p>

<p title="6">School of History</p>
</div>

<div class="three-quarters-column matchingOptions">
<div title="1">
<li>
<h4>UG American Studies School Rep</h4>

<p>Vacant</p>
</li>

<li>
<h4>UG American Studies Stage 2</h4>

<p>Vacant</p>
</li>

<li>
<h4>UG American Studies Stage 3</h4>

<p>Vacant</p>
</li>

</div>

<div title="2">
<li>
<h4>UG Architecture Stage 2</h4>

<p>No Valid Votes</p>
</li>
</div>

<div title="3">
<li>
<h4>UG Drama Joint Honours Stage 2</h4>

<p>Tabitha Andrews-Speed</p>
</li>

<li>
<h4>UG Drama Joint Honours Stage 3</h4>

<p>Ellesse Cooke</p>
</li>

<li>
<h4>UG Drama Stage 2</h4>

<p>Sophie Burkhardt</p>
</li>

<li>
<h4>UG Drama Stage 3</h4>

<p>Vacant</p>
</li>

<li>
<h4>UG Drama Stage 4</h4>

<p>Vacant</p>
</li>

<li>
<h4>UG Film Joint Honours Stage 2</h4>

<p>Vacant</p>
</li>

<li>
<h4>UG Film Joint Honours Stage 3</h4>

<p>Vacant</p>
</li>

<li>
<h4>UG Film Stage 2</h4>

<p>Elena Morresi</p>
</li>

<li>
<h4>UG History and Philosophy of Art Stage 3</h4>

<p>Vacant</p>
</li>
</div>

<div title="4">
<h4>UG English School Rep</h4>

<p>Vacant</p>

<h4>UG Creative Writing Stage 2</h4>

<p>Vacant</p>

<h4>UG Creative Writing Stage 3</h4>

<p>Vacant</p>

<h4>UG English Literature Stage 2</h4>

<p>No Valid Votes</p>

<h4>UG English Literature Stage 3</h4>

<p>Vacant</p>
</div>

<div title="5">
<h4>UG Classics and Archaeology Stage 3</h4>

<p>Vacant</p>

<h4>UG Comparative Literature Stage 2</h4>

<p>Vacant</p>

<h4>UG Comparative Literature Stage 3</h4>

<p>Vacant</p>

<h4>UG English Language and Linguistics Stage 2</h4>

<p>Vacant</p>

<h4>UG English Language and Linguistics Stage 3</h4>

<p>Vacant</p>

<h4>UG French Stage 2</h4>

<p>Vacant</p>

<h4>UG German Stage 2</h4>

<p>Vacant</p>

<h4>UG German Stage 3</h4>

<p>Vacant</p>

<h4>UG Hispanic Studies Stage 2</h4>

<p>No Valid Votes</p>

<h4>UG Hispanic Studies Stage 3</h4>

<p>Vacant</p>

<h4>UG Italian Stage 2</h4>

<p>Alice Cripps</p>

<h4>UG Italian Stage 3</h4>

<p>No Valid Votes</p>

<h4>UG Religious Studies Stage 3</h4>

<p>No Valid Votes</p>
</div>

<div title="6">
<h4>UG History Joint Honours Stage 3</h4>

<p>Vacant</p>

<h4>UG History Stage 2</h4>

<p>No Valid Votes</p>

<h4>UG War Studies Stage 3</h4>

<p>Vacant</p>
</div>
</div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以尝试此https://jsfiddle.net/ovd5u25n/1/

更新JS

$('#box').on('keyup',function(){
    var valThis = $(this).val().toLowerCase();    
      if(valThis == ""){
        $('.three-quarters-column li').show();
      } else {
        $('.three-quarters-column li').each(function(i,item){
        var text = $(item).text().toLowerCase();
        console.log($(item).find('p').text());
        if(text.indexOf(valThis) >= 0) {
         $(item).show()         
        }
        else{
            $(item).hide();
        }
      });
    };
  });