チェックボックス編
html側で labelタグを書いとくだけでいい。重宝してる。
html
<label><input name="hogehoge" id="hogehoge" type="checkbox" value="1"></label>
javascript
$(function() {
$('input[type=checkbox]').filter(":checked").each(function() {
$(this).closest('label').css('font-weight', 'bold');
})
.end().on('change', function() {
$(this).prop('checked') ? $(this).closest('label').css('font-weight', 'bold') : $(this).closest('label').css('font-weight', 'normal');
});
});
ラジオボタン編
idとname をhtmlで記載しておくこと。
nameはそのグループで統一させること。
html
<label><input type="radio" value="1" name="hogehoge" id="hogehoge1">hogehoge1</label>
<label><input type="radio" value="2" name="hogehoge" id="hogehoge2">hogehoge2</label>
javascript
$(function() {
$(":radio").click(function(){
var ans_name = ( $( this ).attr( "name" ) );
var ans_id = ( $( this ).attr( "id" ) );
console.log(ans_name);//デバッグ用
console.log(ans_id);//デバッグ用
$('input[name=' + ans_name).closest('label').css('font-weight', 'normal');
$('#' + ans_id).closest('label').css('font-weight', 'bold');
});
});