简体   繁体   English

用单选按钮切换复选框

[英]Toggle checkboxes with radio button

Building on this answer I'm trying to use a radio button to check all checkboxes on toggle but if it's not toggled they should show unchecked or disabled. 在此答案的基础上,我尝试使用单选按钮来选中切换上的所有复选框,但如果未切换,则它们应显示为未选中或已禁用。 They control the layers on map the default should be all on and users can then remove them. 他们控制地图上的图层,默认应全部启用,然后用户可以将其删除。

Here's the fiddle for what I've tried below; 这是我下面尝试过的小提琴

$('input[value="energyRating"]').change(function() {
    if ($(this).is(':checked')){ //radio is now checked
        $('input[id="check2"]').prop('checked', true);
        $('input[id="check"]').prop('checked', false);
        } 
        else { //radio is now checked
        $('input[id="check2"]').prop('checked', false);
        $('input[id="check"]').prop('checked', false);
        }
});

Clarification: in the fiddle if you click the extra radio button no checkboxes should be checked 说明:在小提琴中,如果单击额外的单选按钮,则不应选中任何复选框

The id attribute of an HTML element should be unique, so the repeated use of check and check2 make your HTML invalid. HTML元素的id属性应该是唯一的,因此重复使用checkcheck2会使您的HTML无效。 I would just drop those id attributes. 我只是删除那些id属性。

One way to do what you need, and also avoid code repetition, is to mark the checkbox elements with data attributes which refer to the radiobutton for which it should be checked. 一种执行所需操作并避免代码重复的方法是,用数据属性标记复选框元素,这些数据属性引用应对其进行检查的单选按钮。

Here is how that would look: 外观如下:

 $('input[name="sex"]').change(function() { var selected = $('input[name="sex"]:checked').val(); $('input[name="vehicle"]').each(function () { $(this).prop('checked', $(this).data("rating") === selected); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="a"> <input type="radio" name="sex" value="energyRating">energyRating<br> <input type="radio" name="sex" value="energyRatingByCount">energyRatingByCount<br> <input type="radio" name="sex" value="extra">extra </div> <div id="b"> <input type="checkbox" data-rating="energyRating" name="vehicle" value="Bike1">I have a bike<br> <input type="checkbox" data-rating="energyRating" name="vehicle" value="Car">I have a car <br> <input type="checkbox" data-rating="energyRatingByCount" name="vehicle" value="Bike">I have another bike<br> <input type="checkbox" data-rating="energyRatingByCount" name="vehicle" value="Car">I have another car </div> 

it is better to handle with class name when u have number of items.please check the below code it might help you to select all and reset all. 当您有多个项目时,最好使用类名称进行处理。请检查以下代码,它可能会帮助您选择所有并重置所有代码。

 $('.click').click(function() { //select all radio is now checked $('.event').prop('checked', true); $('.reset').prop('checked', false); }); $('.reset').click(function() { //reset radio is now checked $('.click').prop('checked', false); $('.event').prop('checked', false); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="a"> <input type="radio" class="click" value="energyRating">select all<br> <input type="radio" class="reset" value="extra">reset </div> <div id="b"> <input id ='check' type="checkbox" class="event" value="Bike1">I have a bike<br> <input id ='check' type="checkbox" class="event" value="Car">I have a car <br> <input id ='check2' type="checkbox" class="event" value="Bike">I have another bike<br> <input id ='check2' type="checkbox" class="event" value="Car">I have another car </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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