2011-09-06 49 views
0

我正在嘗試創建一個功能,可以檢查並取消選中標籤單擊上的複選框。Jquery幫助檢查並取消選中標籤單擊

這裏是我的jsfiddle:http://jsfiddle.net/PTAFG/1/

的HTML不能改變

我的HTML:

<div style="float: left; border: 1px solid rgb(255, 153, 0); width: 198px; margin-top: 2px; background: none repeat scroll 0% 0% rgb(255, 255, 255);"> 
    <label style="float: left; width: 198px; background: url(&quot;../images/ulabel.png&quot;) repeat-x scroll 0% 0% transparent; height: 35px; margin: 0pt; position: relative;"> 
     <span style="display: block; font-family: Helvetica; font-weight: bolder; margin-left: 20px; margin-top: 10px; font-size: 12px; width: 120px;">UBGRÆNSET TRAFIK</span> 
     <input type="hidden" value="0" name="search[php_is_true]"><input type="checkbox" value="1" style="position: absolute; right: 26px; top: 12px;" name="search[php_is_true]" id="search_php_is_true"> 
    </label> 

    <label style="float: left; width: 198px; background: url(&quot;../images/ulabel.png&quot;) repeat-x scroll 0% 0% transparent; height: 35px; margin: 0pt; position: relative; border-top: 1px solid rgb(255, 153, 0); border-bottom: 1px solid rgb(255, 153, 0);"> 
     <span style="display: block; font-family: Helvetica; font-weight: bolder; margin-left: 20px; margin-top: 10px; font-size: 12px; width: 120px;">UBGRÆNSET PLADS</span> 
     <input type="hidden" value="0" name="search[php_is_true]"><input type="checkbox" value="1" style="position: absolute; right: 26px; top: 12px;" name="search[php_is_true]" id="search_php_is_true"> 
    </label> 

     <label style="float: left; width: 198px; background: url(&quot;../images/ulabel.png&quot;) repeat-x scroll 0% 0% transparent; height: 35px; margin: 0pt; position: relative;"> 
     <span style="display: block; font-family: Helvetica; font-weight: bolder; margin-left: 20px; margin-top: 10px; font-size: 12px; width: 120px;">HJEMMESIDEPROGRAM</span> 
     <input type="hidden" value="0" name="search[php_is_true]"><input type="checkbox" value="1" style="position: absolute; right: 26px; top: 12px;" name="search[php_is_true]" id="search_php_is_true"> 
    </label> 
    </div> 

我的Jquery:

$(document).ready(function() { 

$('div').click(
    this.closest('input[type="checkbox"]').attr('checked', true); 

}); 
    $t.closest('.checkGroup').find('.payload').toggle($t.is(':checked')); 
    if(!$t.is(':checked')){ 
     $t.closest('.checkGroup').find('.payload input[type="checkbox"]') 
      .attr('checked',false); 
    } 
}).trigger('change'); 


}); 

回答

1

您可以使用jQuery的道具功能改變複選框。該代碼將是

$('label').bind('click',function(){ 
    var input = $(this).find('input'); 
    if(input.prop('checked')){ 
    input.prop('checked',false); 
    }else{ 
    input.prop('checked',true); 
    } 
}); 
+0

我曾嘗試你的代碼,但該複選框沒有被觸發我曾嘗試選擇「分區」和我差不多完成HTTP標籤http://jsfiddle.net/PTAFG/3/ –

+0

: //jsfiddle.net/PTAFG/18/我只是一個直接點擊複選框時的錯誤 –

+1

這是由於事件冒泡到標籤。這將解決它:http://jsfiddle.net/PTAFG/31/ – Jeremy