【发布时间】:2013-12-04 23:20:35
【问题描述】:
我正在尝试在我的网站上进行实时搜索,我已经让它在输入字段和链接标题匹配的地方工作,但它必须是整个单词。是否可以在下面的代码中显示包含变量 V 的特定值的任何结果:
HTML:
<form id="live-search" action="" class="styled" method="post">
<input type="text" class="text-input" id="filter" value="" />
</form>
<div class="videoItems">
<a title="Oliver" href="#" class="comedy"><img src="#" class="coverImg" alt="" /></a>
<a title="Frank" href="#" class="scifi"><img src="#" class="coverImg" alt="" /></a>
<a title="Ted" href="#" class="scifi"><img src="#" class="coverImg" alt="" /></a>
<a title="James" href="#" class="scifi"><img src="#" class="coverImg" alt="" /></a>
</div>
jQuery:
$("#filter").keyup(function(){
var v = $('#filter').val().toLowerCase();
if ( $('#filter')[0].value.indexOf('> <') !== -1 ){
$('.videoItems a').show();
}
console.log($('#filter').val());
if($('#filter').val() == ''){
$('.videoItems a').show();
}else{
$('.videoItems a').each(function(){
var name = $(this).attr('title').toLowerCase();
if(name == v){
$(this).show();
}else{
$(this).hide();
}
});
}
});
我也发了jsfiddle
提前感谢您的帮助。
【问题讨论】:
标签: jquery html search each contains