简体   繁体   English

需要帮助来优化单选按钮的代码

[英]Need help to optimze a code for radio button

$('input[type="radio"]').change(function(){
    if ($('#subscribe').is(':checked')){
         $("#subNow").show();
         $('#oneTime').hide();
    }
    if ($('#one-time').is(':checked')){
       $('#oneTime').show();
       $("#subNow").hide();
    }   
});

The above code is to show and hide 2 div's on click of 2 radio buttons. 上面的代码是在单击2个单选按钮时显示和隐藏2 div的代码。 I am new to jquery so would like to know is there a better way to write this same functionality. 我是jquery新手,所以想知道是否有更好的方法来编写此相同功能。

If you setup the initial state, say #subNow { display: none; } 如果您设置了初始状态,请说#subNow { display: none; } #subNow { display: none; } , then just use the .toggle() method, no conditions check what-so-ever... #subNow { display: none; } ,则只需使用.toggle()方法,就不会有条件检查任何情况……

 $('input[type="radio"]').change(function(){ $("#subNow").toggle(); $('#oneTime').toggle(); }); 
 #subNow { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <input type='radio' name='rd' checked />1 <input type='radio' name='rd' />2 <br /> <div id="subNow">subNow</div> <div id="oneTime">oneTime</div> 

Also, 也,

if perhaps they are followed on the markup, why not drop JS and go CSS-only?? 如果在标记上遵循它们,为什么不删除JS而仅使用CSS?

 input[type="radio"]:nth-child(2):checked ~ #oneTime, #subNow { display: none; } input[type="radio"]:nth-child(2):checked ~ #subNow{ display: block; } 
 <input type='radio' name='rd' checked />1 <input type='radio' name='rd' />2 <br /> <div id="subNow">subNow</div> <div id="oneTime">oneTime</div> 

This is exactly the way I would do this. 这正是我这样做的方式。 The only other option would be to get the value of the radio buttons, but you'd essentially be doing the same thing that you already are. 唯一的其他选择是获取单选按钮的值,但实际上您会做的是已经做过的事情。

One way is to use common classes and data attributes 一种方法是使用通用的类和数据属性

  $(".rads").on("change", '[type="radio"]', function (e) { var show = $(this).data("show"); $(".details").hide().filter(show).show(); }); 
 .details { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="rads"> <input type="radio" id="x" name="rad" data-show=".foo" /> <label for="x">A</label> <input type="radio" id="y" name="rad" data-show=".bar" /> <label for="y">B</label> </div> <div class="foo details">Apple</div> <div class="bar details">Bacon</div> 

There is also pure CSS solutions using the :checked selector. 也有使用:checked选择器的纯CSS解决方案。

If you don't have any other states to worry about, you can cut the second if and place a else 如果你没有任何其他国家担心,你可以减少第二if和放置else

$('input[type="radio"]').change(function(){
    if ($('#subscribe').is(':checked')){
         $("#subNow").show();
         $('#oneTime').hide();
    }
    else{
       $('#oneTime').show();
       $("#subNow").hide();
    }   
});

You can also use switch 您也可以使用开关

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

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