2012-12-14 162 views
2

我碰到了奇怪的行为。我有一组复选框输入。本身下面的代码工作正常。但是,如果将它集成到网站,当你按下全部按钮没有发生css防止事件

$("[name*=group_]").live('click', function(){ 
     var id = $(this).attr('name').substr(6,1); 
     var cnt = $("[value!='all'][name*=group_"+id+"]:checked").length; 

     if(cnt > 0){ 
      $("[value='all'][name*=group_"+id+"]").removeAttr('checked'); 
     } else { 
      $("[value='all'][name*=group_"+id+"]").attr('checked', 'checked'); 
     } 
    }) 

<fieldset class="subject"> 


         <input type="checkbox" value="all" name="group_4" id="group_4"><label title="All" for="all">All</label> 
              <input type="checkbox" value="15" id="cat_15" name="group_4[]"><label title="Maths" for="cat_15">Maths</label> 

              <input type="checkbox" value="16" id="cat_16" name="group_4[]"><label title="English" for="cat_16">English</label> 

              <input type="checkbox" value="14" id="cat_14" name="group_4[]"><label title="Science" for="cat_14">Science</label> 

        </fieldset> 

请去那里http://jsfiddle.net/XXQTT/1/Applicability尝试。当你点击例如:Early Years它可以工作,但是如果你点击All - 什么都没有。我想它的CSS,因为但无法弄清楚如何,以及如何使其工作

+2

现场()是一个过时功能。你应该使用on(),因为这段代码使用jquery 1.9.1。但是,这可能不是你的问题,只是一个评论... – jtheman

+0

@jtheman我知道,那里有,只是我认为不同的选择和想法也许生活的帮助,只是还没有改变它。但这不是问题 –

+0

@JohnTravolta:阐述'jtheman'的陈述。你不应该使用jQuery的depracated特性的原因是特性markes被depicated通常被更有效的方式所取代。此外,可以随时在任何下一个版本中删除具有特征**的功能,而不会有任何警告。有关详细信息,请查看[depracated item list](depracated item list)(http://api.jquery.com/category/deprecated/)。通常阅读jQuery在线文档是强烈推荐的东西,特别是阅读它非常容易阅读,因为它写得很好。 – Nope

回答

1

的问题[值=“全部”!] - 属性选择没有!选项。 尝试从John Travolta$(":not([value='all'])[name*group_"+id+"]:checked")

编辑

意见的基础上替换它:

  1. 这对我的作品,并返回正确的值。 2.问题没有发生。我把警报,当你 点击所有

不够公平有没有弹出 - 知道得越多! :d有没有在所有标签上的事件冒泡,与测试:

$('label').click(function() { 
    window.alert('hi'); 
}); 

$('[type=checkbox]').click(function() { 
    window.alert('hello'); 
}); 

@edit - 我会责怪脚本处理标签/输入点击,而不是CSS,如下使得它做工精细:

$('label').click(function() { 
    $(this).prev("input:first").click(); 
}); 
+0

jQuery提供[attribute-not-equal](http://api.jquery.com/attribute-not-equal-selector/)选择器。这不是最有效的方法,但它仍然是有效的代码。 –

+0

1.这适用于我,并返回正确的值。 2.问题没有发生。我把警报,并没有弹出,当你点击所有 –

+0

公平的 - 你知道得越多! :D 所有标签上都没有冒泡事件,用'$('label')测试。click(function(){window.alert('hi');}); $( '[类型=复选框]')点击(函数(){window.alert( '你好');});' @edit - 我归咎于脚本处理标签/输入点击,不css,作为'$('label')。click(function(){$(this).prev(“input:first”)。click();});'使它工作正常 – eithed