Jquery帮助检查并取消选中标签点击

时间:2011-09-06 14:54:09

标签: javascript 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 个答案:

答案 0 :(得分:1)

您可以使用jQuery prop函数更改复选框。代码是

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