简体   繁体   中英

How to validate checkboxes in particular div using JQuery?

How to check whether checkbox in particular DIV is checked or not?

<div id="TeddyTab">
 <input type="checkbox" id="bla" />
 <input type="checkbox" id="bla1" />
 <input type="checkbox" id="bla2" />
</div>

JQuery?

 $(':checked').each(function() {
        if(this.checked === 'true')
            $(this).closest('div').next('div').find('textarea').prop('readonly',false);
    });

The above jquery checkes everything n page which i do not want. I want to check in above div only?

use the below code to validate in particular div

 if($("#TeddyTab input:checked").size() > 0){
// do here what ever you wanna do
}

you are comparing boolean with string value as this.checked return boolean value. also you are only iterating over checked element which eliminates the need of using if condition

$(':checked').each(function() {
    $(this).closest('div').next('div').find('textarea').prop('readonly',false);
});

Without much changes; Here is how it worked:

$('#TeddyTab :checkbox').each(function() {
 if(this.checked)
 $(this).closest('div').next('div').find('textarea').prop('readonly',false);
});

Posible duplicated jQuery if checkbox is checked ? Anyway, this is how you should implement it:

$('#TeddyTab input[type="checkbox"]:checked').each(function(i,input){
      $(input).closest('div').next('div').find('textarea').prop('readonly',false);

or if you need the IF statement:

$('#TeddyTab input[type="checkbox"]').each(function(i,input){
            if($(input).is(':checked')){ 
                $(input).closest('div').next('div').find('textarea').prop('readonly',false);

Try This :

DEMO : http://jsfiddle.net/K6Wvk/

$(document).ready(function () {

$('#formid').validate({ // initialize the plugin
    rules: {
        'test[]': {
            required: true,
            maxlength: 2
        }
    },
    messages: {
        'test[]': {
            required: "You must check at least 1 box",
            maxlength: "Check no more than {0} boxes"
        }
    }
});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM