简体   繁体   English

jQuery removeClass似乎不起作用

[英]jquery removeClass doesn't seem to work

While firstli.hasClass('selected') does work and retrieves the correct element. 虽然firstli.hasClass('selected')可以工作并检索正确的元素。 The removeClass function doesn't seem to remove to the class. removeClass函数似乎并未删除到该类。 When an element is unchecked I would like the first li to be unchecked as well. 当一个元素未被选中时,我也希望第一个li也被选中。

Here is the jfiddle (the code is at the bottom) 这是jfiddle (代码在底部)

Note: I want "Select All" to become unchecked when you uncheck one of the other options. 注意:当您取消选中其他选项之一时,我希望不选中“全选”。

var firstli = $('.dropdown-menu.inner li').first();
firstli.click(function(event) {   
    if (!firstli.hasClass('selected')) {    
        $('.selectpicker').selectpicker('selectAll');       
    }
    else {
        $('.selectpicker').selectpicker('deselectAll');
    }
    return false;
});

var alllis = $('.dropdown-menu.inner li:not(:first-child)');

alllis.click(function(event) {
    if ($(this).hasClass('selected')) {    
        alert(firstli.hasClass('selected')); //true
        firstli.removeClass('selected');
    }
});

Your problem is with setSelected() rechecking the Select All option. 您的问题在于setSelected()检查“全选”选项。 To fix this, I changed that function to the following: 为了解决这个问题,我将该函数更改为以下内容:

setSelected: function (c, d) {
    if (d) {
        this.$menu.find("li").eq(c).addClass("selected")
    } else {
        this.$menu.find("li").eq(c).removeClass("selected")
        this.$menu.find("li:first-child").removeClass("selected")
    }
}

Basically, if it has to remove the selected class, you know they're not all checked. 基本上,如果必须删除selected类,则知道它们并未全部选中。 This works. 这可行。

http://jsfiddle.net/eC8hF/30/ http://jsfiddle.net/eC8hF/30/

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

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