javascript - 实现搜索栏以过滤列表项

标签 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>

最佳答案

你可以试试这个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();
        }
      });
    };
  });

关于javascript - 实现搜索栏以过滤列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614671/

相关文章:

Javascript RegExp 用于将文本分割成句子并保留分隔符

jquery - 亨德尔的音量 slider 没有显示?

javascript - 预填充 jsrender 模板

html - Bootstrap 条纹下拉菜单

javascript - 在 Redux 容器中使用高阶组件

javascript - 我的图像元素和 React 的源问题

html - 表单输入的奇怪 iOS 设备特定问题

CSS - Flexbox 网格上的背景颜色 - 列布局 - 顶行,第一列

javascript - 设置目标后如何重新启动 Orbit Controls

javascript - 将类应用于具有属性的元素