我用漂亮的复选框插件玩,并有问题。 插件可以在这里看到http://aaronweyenberg.com/90/pretty-checkboxes-with-jquerytoggleClass搞砸了一些东西
正如你所看到的,这使用两个链接来选择和取消选择。我希望它只使用一个链接,然后改变类,使其在下次单击时以另一种方式工作。
它应该很简单。我一直试图让它与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");
}
);
});
此作品在一定程度上。这些类切换得很好,但其余的代码只在第一次运行时才起作用。随后的所有点击只是切换链接的类别,而没有其他内容。
有没有人有任何想法,为什么这是?
非常感谢。我应该提到,我是JavaScript和jquery的新手。所以我确实认为它应该每次都进行评估。但是现在我知道一些关于事件监听器的东西,所以谢谢你,你和Pointy。 真的很好的例子,但它有一个小呃。我不知道这是否是故意的,但我只能将一个元素标记为选中状态,就像单选按钮一样。 所以我改了一行: var selected = $('。checklist input:checked')。length!== 0; 至: var selected = $(this).siblings(':checkbox')。attr('checked'); 现在它工作得很好。 :d – 2010-04-22 21:34:58