2012-12-14 95 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