show hide content depending if a checkbox is checked

霸气de小男生 提交于 2019-12-05 17:00:29
Reigel

you can use :checked as a selector. like this,

alert($(':checkbox:checked').attr('id'));

updated fiddle

or if you want to check the state of a particular checkbox, it would be something like this

alert($('#set_1')[0].checked);

updated fiddle


for the comment below, I have edited your code and now looks like this.

$('.wpbook_hidden').hide();

$(':checkbox').change(function() {
    changeCheck(this);
});

$(':checkbox:checked').each(function(){ // on page load, get the checked checkbox.
    changeCheck(this); // apply function same as what is in .change()
});

function changeCheck(x) {
    var option = 'wpbook_option_' + $(x).attr('id');
    if ($('.' + option).is(':visible')) { // I'm not sure about my if here.
        $('.' + option).fadeOut();        // if necessary try exchanging the fadeOut() and fadeIn()
    }
    else {
        $('.' + option).fadeIn();
    }
}

#updated fiddle

to checkout if a checkbox is checked you can use the following code :

$(':checkbox').change(function() {
  if ( $(this).checked == true ){
     //your code here
   }

});
phungdinhvu

Try this:

$("input[type=checkbox]:checked").each(fun....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!