切换类搞乱了一些东西
我正在使用 Pretty-checkboxes-plugin 并遇到问题。 该插件可以在这里看到 http://aaronweyenberg.com/90/pretty-checkboxes- with-jquery
如您所见,这使用两个链接来选择和取消选择。我希望它只使用一个链接,然后更改类以使其在下次单击时以另一种方式工作。
它应该很简单。我一直在尝试让它与toggleClass 一起工作。
我只是放入了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");
}
);
});
这在某种程度上有效。类切换得很好,但其余代码仅在第一次运行时有效。所有后续点击都只是切换类和链接,仅此而已。
有谁知道这是为什么吗?
I am using playing around with the Pretty-checkboxes-plugin and have a problem.
The plugin can be seen here 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.
I simply put in the toggleClass statement so the code looks like so:
$(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅更改元素的类不会更改在其上注册的事件侦听器。您注册了用于在准备时选择与
$(".checklist .checkbox-select")
匹配的元素的处理程序,并且该处理程序(而不是取消选择处理程序)在元素更改为.checkbox-select
到checkbox-deselect
。如果您确实需要事件时间绑定,则需要
live()
函数而不是普通的click()
绑定。但是,我建议您最好使用单个函数: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
tocheckbox-deselect
.If you really want event-time binding, you need the
live()
function instead of plainclick()
binding. However, I suggest you'd probably be better off with a single function:您设置事件处理程序的方式让我认为您正在想象将根据“类”设置动态调用处理程序。您实际上得到的是在调用“就绪”处理程序时根据类设置静态绑定处理程序的东西。 类分配的更改对这些处理程序没有任何影响。处理程序被调用。
除了这种设置之外,还有几种方法可以选择。首先,您可以绑定一个事件处理程序,然后使用简单的“hasClass()”测试检查该处理程序内的当前类。那会很简单。
另一种方法是使用“委托”或“实时”机制,它们确实具有当前代码似乎希望的动态解释行为。
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. 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.