2009-10-29 139 views
4

我有一個帶複選框的頁面,其中一個是全選按鈕。因此,當用戶單擊全選時,它將檢查所有其他框,並在單擊另一個框時取消選中全部。這是代碼。jQuery複選框事件不會觸發?

  $(document).ready(function(event){ 
       //alert('wtf'); 
      $('#mailSubscribeAll').click(function(event){ 
       //alert('wtf'); 
       $('.mailingCheckbox').attr('checked','checked'); 
      });    

      $('.mailingCheckbox').bind('click',function(event){ 
       //alert('wtf'); 
       $('#mailSubscribeAll').removeAttr('checked'); 
      }); 

     }); 

所以我的頁面加載jQuery罰款,第一個警報觸發器。沒有任何點擊事件會提供警報。我已經仔細檢查了我的HTML是否正確。

所以,我使用jQuery的錯誤事件,或者是否有可能在我的網頁與其他非jQuery的JavaScript的衝突?

HTML片段:

<tr> 
    <td class="CBT3" colspan="3" height="29"> 
     <input type="checkbox" class="mailingCheckbox" id="mailNewsAlerts" value="1" {if $smarty.session.profile.mailNewsAlerts}checked{/if} onclick="subscribeMarkProfile()"> 
     <label for="mailNewsAlerts"><b>News Alerts</b> - <cite>The latest breaking news from XXXXXXX</cite></label> 
    </td> 
</tr>  
<tr> 
    <td class="CBT3" colspan="3" height="29"> 
     <input type="checkbox" id="mailSubscribeAll" value="1" {if $smarty.session.profile.mailSubscribeAll}checked{/if} > 
     <label for="mailSubscribeAll"><b>Subscribe me to all XXXXX newsletters!</b> </label> 
    </td> 
</tr>  

我會還與複選框添加頁面的部分不是在頁面加載時可見的,但它的存在於HTML。我點擊按鈕後顯示區域,但當頁面加載時,首先這些複選框是不可見的。

+0

有可用的HTML片段嗎? – jvenema 2009-10-29 17:46:27

+0

補充說,這段代碼亂七八糟,所有都在表格中,而且我粘貼了聰明的模板代碼,而不是渲染的HTML,但我檢查並且它也呈現罰款 – tkotitan 2009-10-29 17:54:58

+0

,技術上我的輸入字段不在

標記中,如果事項。 – tkotitan 2009-10-29 17:57:06

回答

1

它如預期的我下面的代碼工作:

<div> 
    <input type="checkbox" id="mailSubscribeAll"/> 
    <input type="checkbox" class="mailingCheckbox"/> 
    <input type="checkbox" class="mailingCheckbox"/> 
</div> 

<script type="text/javascript"> 
    $(document).ready(function(event){ 
    $('#mailSubscribeAll').click(function(){ 
     $('.mailingCheckbox').attr('checked','checked'); 
    });    
    $('.mailingCheckbox').click(function(){ 
     $('#mailSubscribeAll').removeAttr('checked'); 
    }); 
    });  
</script> 

這是否闡明你的問題任何光線?

1
$("input:checkbox #mailSubscribeAll").click(function(event){ 
    $('.mailingCheckbox').attr('checked','checked'); 
}); 

它看起來不錯,但你現在可以試試這個代碼。

1

當編程更改複選框的狀態,還必須檢查事件change,點擊是不是始終點火:

$('.mailingCheckbox').bind('change', ... 

,因爲你有兩個功能,你應該這樣做:

function changeMailingCheckbox(...) { ... } 
$('.mailingCheckbox').bind('change', changeMailingCheckbox) 
$('.mailingCheckbox').bind('click', changeMailingCheckbox) 
+0

謝謝你,試過了,沒有運氣。我認爲我的頁面是如此混亂的表,可能jQuery失去了它的頭腦在看着它? – tkotitan 2009-10-29 18:20:38