简体   繁体   English

如何根据jquery中的过滤器禁用和启用列表项?

[英]How do I disable and enable list items depending on the filter in jquery?

Here is what I've created: http://jsfiddle.net/zidski/ktSA9/ 这是我创建的: http : //jsfiddle.net/zidski/ktSA9/

I want to enable and disable the links depending on the filter - 'All Years' starts off disabled as all the years are showing - if I click on 2010 All years should become enabled and so on. 我想根据过滤器启用和禁用链接-“所有年份”一开始显示为禁用,因为所有年份都显示-如果我单击2010,则所有年份都应启用,依此类推。

<ul name="yearfilter" id="yearfilter">
<li value=""><a data-value="" href="#" class="disable">All years</a></li>
<li value="2011"><a data-value="2011" href="#">2011</a></li>
<li value="2010"><a data-value="2010" href="#">2010</a></li>
</ul>

I have updated your fiddle: http://jsfiddle.net/ktSA9/4/ 我已经更新了您的小提琴: http : //jsfiddle.net/ktSA9/4/

Pertinent change: 相关更改:

$("#yearfilter a").removeClass("disable");
$(this).addClass("disable");

Something like this should do the trick: 这样的事情应该可以解决问题:

$("#yearfilter a").bind('click', function() {
    $("#yearfilter a").removeClass('disable');
    $(this).addClass('disable');
});

EDIT: Changed the above to bind to the anchor elements instead of the list items. 编辑:更改以上内容绑定到锚元素,而不是列表项。

Essentially you just need to add the following two lines to your existing function: 本质上,您只需要在现有函数中添加以下两行:

$("#yearfilter a").removeClass('disable');
$(this).addClass('disable');

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

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