简体   繁体   中英

Check the checkbox with given value is checked in jquery

I have the checkboxes and I want to check the checkbox with specific value id checked in jquery I want to check the check box meetingcategory with value ' test ' is checked if checked need to uncheck the relatedto checkbox lead and contacts.

 $(".meetingcategory input").click(function(){ if($(this).val()=='test') //need to check is checked { $('.relatedto input[value="Leads"]').prop('checked', false); $('.relatedto input[value="Contacts"]').prop('checked', false); } else{ $('.relatedto input[value="Leads"]').prop('checked', true); $('.relatedto input[value="Contacts"]').prop('checked', true); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='meetingcategory'> <input type="checkbox" title="test" id="ms-opt-124" value="test">test <input type="checkbox" title="test1" id="ms-opt-124" value="test1">test1 </div> <div id='relatedto'> <label for="ms-opt-1"><input type="checkbox" title="Leads" id="ms-opt-1" value="Leads" checked>Leads</label> <label for="ms-opt-1"><input type="checkbox" title="Contacts" id="ms-opt-1" value="Contacts" checked>Contacts</label> <label for="ms-opt-1"><input type="checkbox" title="Blank" id="ms-opt-1" value="Blank" checked>Blank</label> </div>

 $(".meetingcategory input").click(function() { if ($(this).val() == 'test') //need to check is checked { $('input[value="Leads"]').prop('checked', false); $('input[value="Contacts"]').prop('checked', false); } else { $('input[value="Leads"]').prop('checked', true); $('input[value="Contacts"]').prop('checked', true); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='meetingcategory'> <input type="checkbox" title="test" id="ms-opt-124" value="test">test <input type="checkbox" title="test1" id="ms-opt-124" value="test1">test1 </div> <div id='relatedto'> <label for="ms-opt-1"><input type="checkbox" title="Leads" id="ms-opt-1" value="Leads" checked>Leads</label> <label for="ms-opt-1"><input type="checkbox" title="Contacts" id="ms-opt-1" value="Contacts" checked>Contacts</label> <label for="ms-opt-1"><input type="checkbox" title="Blank" id="ms-opt-1" value="Blank" checked>Blank</label> </div>

I am not sure, why are you using ".relatedto" in the selector, this is working fine.

No need this .relatedto. you can use the only attribute

 $(".meetingcategory input").click(function(){ if($(this).val()=='test') //need to check is checked { $('.relatedto input[value="Leads"]').prop('checked', false); $('.relatedto input[value="Contacts"]').prop('checked', false); } else{ $('.relatedto input[value="Leads"]').prop('checked', true); $('.relatedto input[value="Contacts"]').prop('checked', true); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='meetingcategory'> <input type="checkbox" title="test" id="ms-opt-124" value="test">test <input type="checkbox" title="test1" id="ms-opt-124" value="test1">test1 </div> <div id='relatedto'> <label for="ms-opt-1"><input type="checkbox" title="Leads" id="ms-opt-1" value="Leads" checked>Leads</label> <label for="ms-opt-1"><input type="checkbox" title="Contacts" id="ms-opt-1" value="Contacts" checked>Contacts</label> <label for="ms-opt-1"><input type="checkbox" title="Blank" id="ms-opt-1" value="Blank" checked>Blank</label> </div>

 $(".meetingcategory input").click(function(){ if($(this).val()=='test') //need to check is checked { $('input[value="Leads"]').prop('checked', false); $('input[value="Contacts"]').prop('checked', false); } else{ $('input[value="Leads"]').prop('checked', true); $('input[value="Contacts"]').prop('checked', true); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='meetingcategory'> <input type="checkbox" title="test" id="ms-opt-124" value="test">test <input type="checkbox" title="test1" id="ms-opt-124" value="test1">test1 </div> <div id='relatedto'> <label for="ms-opt-1"><input type="checkbox" title="Leads" id="ms-opt-1" value="Leads" checked>Leads</label> <label for="ms-opt-1"><input type="checkbox" title="Contacts" id="ms-opt-1" value="Contacts" checked>Contacts</label> <label for="ms-opt-1"><input type="checkbox" title="Blank" id="ms-opt-1" value="Blank" checked>Blank</label> </div>

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