简体   繁体   English

toggleClass搞砸了

[英]toggleClass messing something up

I am using playing around with the Pretty-checkboxes-plugin and have a problem. 我在玩Pretty-checkboxes-plugin时遇到问题。 The plugin can be seen here http://aaronweyenberg.com/90/pretty-checkboxes-with-jquery 可以在这里看到该插件http://aaronweyenberg.com/90/pretty-checkboxes-with-jquery

As you can see, this uses two links to select and deselect. 如您所见,这使用两个链接进行选择和取消选择。 I want it to only use one link and then change the class to make it work the other way the next time it is clicked. 我希望它仅使用一个链接,然后更改类以使其在下次单击时以另一种方式工作。

It should be simple enough. 它应该足够简单。 I have been trying to get it working with toggleClass. 我一直在尝试使其与toggleClass一起使用。

I simply put in the toggleClass statement so the code looks like so: 我只是放入toggleClass语句,所以代码如下所示:

$(document).ready(function() {

/* see if anything is previously checked and reflect that in the view*/
$(".checklist input:checked").parent().addClass("selected");

/* handle the user selections */
$(".checklist .checkbox-select").click(
function(event) {
  event.preventDefault();
  $(this).parent().addClass("selected");
  $(this).parent().find(":checkbox").attr("checked","checked");
  $(this).toggleClass("checkbox-select checkbox-deselect");

 }

);

$(".checklist .checkbox-deselect").click(
 function(event) {
  event.preventDefault();
   $(this).parent().removeClass("selected");
   $(this).parent().find(":checkbox").removeAttr("checked");
   $(this).toggleClass("checkbox-select checkbox-deselect");
   }

   );

});

This works to some extend. 这在一定程度上起作用。 The classes toggle just fine, but the rest of the code only works the first time it is run. 这些类可以很好地切换,但是其余代码仅在第一次运行时起作用。 All subsequent clicks just toggle the class og the link and nothing else. 以后所有的单击都仅使类切换链接,而没有其他任何事情。

Does anyone have any idea why this is? 有谁知道为什么会这样吗?

The way you've set up your event handlers makes me think that you are imagining that the handlers will be invoked based on the "class" setup dynamically. 设置事件处理程序的方式使我认为您正在想象将基于“类”设置动态调用处理程序。 What you've actually got there is something that statically binds handlers based on the class setting at the time that the "ready" handler is called. 实际上,在调用“就绪”处理程序时,有些东西会根据类设置静态绑定处理程序。 The changing of the class assignment does nothing to the way that those handlers are invoked. 类分配的更改与那些处理程序的调用方式无关。

Instead of that setup, there are a couple of ways to go. 除了该设置之外,还有两种方法可供选择。 First, you could bind a single event handler and then check the current class inside that handler with a simple "hasClass()" test. 首先,您可以绑定一个事件处理程序,然后使用简单的“ hasClass()”测试检查该处理程序中的当前类。 That would be pretty simple. 那将非常简单。

Another approach would be to use the "delegate" or "live" mechanisms, which really do have the dynamic interpretation behavior that your current code seems to wish for. 另一种方法是使用“委托”或“实时”机制,它们确实具有您当前代码似乎希望的动态解释行为。

$(this).toggleClass("checkbox-select checkbox-deselect");

Just changing an element's class doesn't change the event listeners registered on it. 仅更改元素的类不会更改在其上注册的事件侦听器。 You registered the handler for selecting on elements matching $(".checklist .checkbox-select") at ready-time and that handler, not the deselecting handler, is still in place after the element has changed from .checkbox-select to checkbox-deselect . 您已在就绪时注册了用于选择与$(".checklist .checkbox-select")匹配的元素的处理程序,并且在元素从.checkbox-select更改为checkbox-deselect之后,该处理程序(而不是取消选择处理程序)仍然存在。 checkbox-deselect

If you really want event-time binding, you need the live() function instead of plain click() binding. 如果您确实需要事件时绑定,则需要live()函数而不是普通的click()绑定。 However, I suggest you'd probably be better off with a single function: 但是,我建议您最好使用一个函数:

$('#checktoggler').click(function() {
    // select all, unless all are already selected, in which case select none
    var selected= $('.checklist input:checked').length!==0;
    selected= !selected;

    $(this).parent().toggleClass('selected', selected);
    $(this).parent().find(":checkbox").attr('checked', selected);
    $(this).toggleClass('checkbox-select checkbox-deselect', selected);
});

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

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