简体   繁体   English

与li的jquery复选框

[英]jquery checkbox with li

I have a series of checkboxes, and everything works except their default behavior has been altered unintentionally so I can no longer check them which is odd since the reason I was using this bit of jquery was to highlight the li around them when they got checked in the first place. 我有一系列的复选框,一切正常,除了他们的默认行为已被无意改变,所以我不能再检查它们是奇怪的,因为我使用这个jquery的原因是当他们检查时突出他们周围的李第一名。

Any ideas? 有任何想法吗?

//Tag cloud
$(".tag-cloud li").toggle(
  function () {
    //$(this).filter(":checkbox").attr('checked', 'true');
    $(this).addClass("selected");
    return;
    //return $(this).filter(":checkbox").attr("checked", true);
  },
  function () {
    //$(this).filter(":checkbox").attr('checked', 'false');
    $(this).removeClass("selected");
    return;
    //$("#sortable").submit();
  }
);

You can simplify it overall and hopefully eliminate the odd behavior by using a simple .click() event handler, like this: 您可以整体简化它,并希望通过使用简单的.click()事件处理程序消除奇怪的行为,如下所示:

$(".tag-cloud li").click(function() {
 var cb = $(this).find(":checkbox")[0];
 $(this).toggleClass("selected", cb.checked);
});

This has the benefit of working regardless of what state it was initially in, where as .toggle() will be off for pre-checked boxes. 无论最初处于何种状态,这都具有工作的好处,其中.toggle()将关闭以用于预先选中的框。

If you want the <li> itself clickable, we need to be sure not to get in a loop or reverse the toggle when clicking the actual checkbox itself (if it's exposed), like this: 如果你希望<li>本身可以点击,我们需要确保在单击实际的复选框本身时(如果它已暴露)不进入循环或反转切换,如下所示:

$(".tag-cloud li").click(function(e) {
 var cb = $(this).find(":checkbox")[0];
 //if the click wasn't from the checkbox already, toggle it
 if(e.target != cb) cb.checked = !cb.checked;
 $(this).toggleClass("selected", cb.checked);
});

Try removing the return; 尝试删除return; lines. 线。

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

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