Jquery help check and uncheck on label click
I am trying to create a functionality that check and uncheck the checkbox on label click.
Here is my Jsfiddle: http://jsfiddle.net/PTAFG/1/
The HTML cannot be changed
My 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("../images/ulabel.png") 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: 1开发者_开发百科0px; 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("../images/ulabel.png") 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("../images/ulabel.png") 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>
My 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');
});
You can use the jQuery prop function to change the checkbox. The code would be
$('label').bind('click',function(){
var input = $(this).find('input');
if(input.prop('checked')){
input.prop('checked',false);
}else{
input.prop('checked',true);
}
});
精彩评论