简体   繁体   English

如果选中了两个单选按钮,则将 css 添加到 div

[英]If two radio button checked, add css to div

There are two radio buttons separately.分别有两个单选按钮。 Have different name values.具有不同的名称值。 I'm trying to add a css to the div element when both are "checked".当两者都被“选中”时,我正在尝试将 css 添加到 div 元素。

 $(document).ready(function(){ $('input[name="nameone"]').click(function(){ if($(this).is(":checked")){ $(".calculate-total").css("display","block") } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label> <input type="radio" name="nameone" /> Nameone radio </label> <br><br> <label> <input type="radio" name="nametwo" /> Namtwo radio </label> <div class="calculate-total" style="display:none;">If two radio checked both, this div will be visible.</div>

Just check if both of them are checked:只需检查它们是否都被检查:

$(document).ready(function(){
    $('input[name="nameone"], input[name="nametwo"]').click(function(){
        if($('input[name="nameone"]').is(":checked") && $('input[name="nametwo"]').is(":checked")){
            $(".calculate-total").css("display","block")
        }
    });
});

Just add a second if condition like you do for the input[name="nameone"] :只需添加第二个if条件,就像您为input[name="nameone"]的那样:

 function showDiv() { if ($('input[name="nameone"]').is(":checked") && $('input[name="nametwo"]').is(":checked")) { $(".calculate-total").css("display", "block") } } $(document).ready(function() { $('input[name="nameone"], input[name="nametwo"]').click(function() { showDiv(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label> <input type="radio" name="nameone" /> Nameone radio </label> <br><br> <label> <input type="radio" name="nametwo" /> Namtwo radio </label> <div class="calculate-total" style="display:none;">If two radio checked both, this div will be visible.</div>

You are only checking if the first radio input is clicked (checked), that's why you the div is showing when you click on the first radio input.您只检查是否单击了第一个单选输入(选中),这就是您单击第一个单选输入时显示 div 的原因。 You should listen/check for both.你应该听/检查两者。 If both are clicked (checked).如果两者都被单击(选中)。 Then that's when you want to do something.然后就是你想做某事的时候。 You can use the && to get the job done您可以使用 && 来完成工作

 function showDiv() { if ($('input[name="nameone"]').is(":checked") && $('input[name="nametwo"]').is(":checked")) { $(".calculate-total").css("display", "block") } } $(document).ready(function() { $('input[name="nameone"], input[name="nametwo"]').click(function() { showDiv(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label> <input type="radio" name="nameone" /> Nameone radio </label> <br><br> <label> <input type="radio" name="nametwo" /> Namtwo radio </label> <div class="calculate-total" style="display:none;">If two radio checked both, this div will be visible.</div>

You have to check if all radio buttons are checked and based on this check you can toggle the visibility of the block.您必须检查是否所有单选按钮都被选中,并且基于此检查,您可以切换块的可见性。

I added a class to the radiobuttons you want to check.我在要检查的单选按钮中添加了 class。 And only if the length of all radiobuttons is equal to the checked onces i show the calculate-total div并且只有当所有单选按钮的长度等于检查的一次时,我才会显示计算总计 div

 $(document).ready(function() { $('input[type="radio"]').on('change', function() { if ($('.check-it').filter(':checked').length === $('.check-it').length) { $(".calculate-total").css("display", "block") } else { $(".calculate-total").css("display", "none") } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label> <input type="radio" name="nameone" class="check-it" /> Nameone radio </label> <label> <input type="radio" name="nameone" /> Nameone radio </label> <br><br> <label> <input type="radio" name="nametwo" class="check-it" /> Namtwo radio </label> <label> <input type="radio" name="nametwo" /> Namtwo radio </label> <div class="calculate-total" style="display:none;">If two radio checked both, this div will be visible.</div>

You need to listen for both radio buttons and check for both each time:您需要同时收听两个单选按钮并每次都检查:

 $(document).ready(function(){ $('input:radio').change(function(){ if($('input[name="nameone"]').is(":checked") && $('input[name="nametwo"]').is(":checked")){ $(".calculate-total").css("display","block") } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label> <input type="radio" name="nameone" /> Nameone radio </label> <br><br> <label> <input type="radio" name="nametwo" /> Namtwo radio </label> <div class="calculate-total" style="display:none;">If two radio checked both, this div will be visible.</div>

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

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