简体   繁体   English

从组中选择一个单选按钮,并显示show div

[英]select one radio button from group with the show hide div

I am extending my previous question . 我扩大了我以前的问题 I am trying now if one of the radio button is selected then other should de-select by default. 我现在尝试如果选择了一个单选按钮,则默认情况下应取消选择另一个。

I have tried by adding this : 我尝试通过添加以下内容:

JS JS

function Show_Div(Div_id) {
  if (!$(Div_id).is(':visible')) {
    $(Div_id).prev().children().prop('checked', true);
    $(Div_id).show(250);
  } else {
    $(Div_id).prev().children().prop('checked', false);
    $(Div_id).hide(250);
  }
}
    function cbChange1(obj) {
        var cb1 = document.getElementsByClassName("cb1");
        for (var i = 0; i < cb1.length; i++) {
            cb1[i].checked = false;
        }
        obj.checked = true;
    }

HTML 的HTML

<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/flower-icon.png" alt="" onclick="Show_Div(Div_1)" width="100px">
    <p>
      <input type="radio" onclick="Show_Div(Div_1)" class="cb1" onchange="cbChange1(this)">Flower 1</p>
    <div id="Div_1" style="display: none;">
      Flower is pink.
    </div>
    <br/>
    <br/>

    <img src="http://www.clker.com/cliparts/0/d/w/v/V/p/pink-flower-md.png" alt="" onclick="Show_Div(Div_2)" width="100px">
    <p>
      <input type="radio" onclick="Show_Div(Div_2)" class="cb1" onchange="cbChange1(this)">Flower 2</p>
    <div id="Div_2" style="display: none;">
      Flower is orange.
    </div>

But by above code only the radio buttons are select and de-select. 但是通过上面的代码,只有单选按钮被选择和取消选择。 The show and hide div which is connected with radio buttons is not show and hide by above. 与单选按钮连接的显示和隐藏div不在上方显示和隐藏。

Can anyone help me in this? 有人可以帮我吗?

You're going about it wrong, it's actually quite simple. 您正在处理错误,实际上很简单。 If two radio boxes are effectively answers to the same question then all you need to do is give both elements the same name . 如果两个单选框有效地回答了相同的问题,那么您要做的就是给两个元素起相同的name Then to handle showing and hiding of div 's you can do this - 然后要处理div的显示和隐藏,您可以执行以下操作-

 function Show_Div(Div_id, element) { $("input[name='radio1']").not(element).parent().next('div').hide(250); if (!$(Div_id).is(':visible')) { $(Div_id).prev().children().prop('checked', true); $(Div_id).show(250); } else { $(Div_id).prev().children().prop('checked', false); $(Div_id).hide(250); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/flower-icon.png" alt="" onclick="Show_Div(Div_1)" width="100px"> <p> <input type="radio" name="radio1" onclick="Show_Div('#Div_1')" class="cb1" >Flower 1</p> <div id="Div_1" style="display: none;"> Flower is pink. </div> <br/> <br/> <img src="http://www.clker.com/cliparts/0/d/w/v/V/p/pink-flower-md.png" alt="" onclick="Show_Div(Div_2)" width="100px"> <p> <input type="radio" name="radio1" onclick="Show_Div('#Div_2')" class="cb1" >Flower 2</p> <div id="Div_2" style="display: none;"> Flower is orange. </div> 

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

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