简体   繁体   English

选择单选按钮时显示/隐藏div

[英]Show/hide divs when radio button selection

I have this piece of code which shows me a div defined in data-target attribute after I select radio button (by clicking on a label to be specific), but what I need is that when I choose to change the choice, the other div needs to disappear.. I've tried many things but none of them have worked.. Thanks a lot 我有这段代码,向我展示了选择单选按钮后(通过单击特定的标签)在data-target属性中定义的div,但是我需要的是当我选择更改选择时,另一个div需要消失..我已经尝试了很多事情,但没有一个起作用..非常感谢

When I click on radio Ano1 - div f4 appears and q2 disappears When I click on radio Ne1 - div f4 disappears and q2 appears.. 当我单击收音机Ano1-div f4出现,而q2消失当我单击收音机Ne1-div f4消失,并且q2出现。

And it goes the same way on another div (as you can see).. 它在另一个div上的作用相同(如您所见)。

 $(document).ready(function() { $('[data-target]').click(function() { // $('.none').hide(); if ($(this).is(':hover')) { var target = $(this).attr('data-target'); $(target).show(1000); } }); }); // `$('.none').hide();` is in comment because I want to see the choices.. 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="q1" class="none"> <h3>Žádáte o nové číslo?</h3> <input type="radio" name="novecislo" value="Ano1" id="Ano1"><label for="Ano1" data-target="#f4">Ano</label> <br> <input type="radio" name="novecislo" value="Ne1" id="Ne1"><label for="Ne1" data-target="#q2">Ne</label> </div> <div id="q2" class="none"> <h3>Chcete převést číslo v rámci operátora T-mobile?</h3> <input type="radio" name="op" id="Ano2"><label for="Ano2" data-target="#q3">Ano</label> <br> <input type="radio" name="op" id="Ne2"><label for="Ne2" data-target="#f1,#f4">Ne</label> <br> </div> <div id="f4" class="none"> <h4>Jaký tarif chcete zvolit?</h4> <input type="radio" name="tarif" value="tarif1" id="t1"><label for="t1" title="15Kč | bez dat | volání a sms 0,29/min">Profi na míru 1</label> <br> <input type="radio" name="tarif" value="tarif2" id="t2"><label for="t2" title="190Kč | 1,5GB | volání a sms zdarma">Profi na míru 2</label> <br> <input type="radio" name="tarif" value="tarif3" id="t3"><label for="t3" title="229Kč | 5GB | volání a sms zdarma">Profi na míru 3</label> <br> <input type="radio" name="tarif" value="tarif5" id="t5"><label for="t5" title="149Kč | bez dat | volání a sms zdarma">Profi na míru 5</label> <br> <h4>Co chcete aktivovat?</h4> <input type="checkbox" name="akt" value="roaming" id="cc1"><label for="cc1">Roaming</label> <br> <input type="checkbox" name="akt" value="platby" id="cc2"><label for="cc2">Platby</label> <p></p> <input type="button" name="send" value="ODESLAT" onclick="vypisForm();"> </div> 

You can do one simple trick: Hide all divs which have class none before you make any one visible. 你可以做一个简单的一招:隐藏具有类的所有div none你做任何一个可见之前。 Like this: 像这样:

 $(document).ready(function() {
  $('[data-target]').click(function() {
    // $('.none').hide();
    if ($(this).is(':hover')) {
      $('.none').hide();
      var target = $(this).attr('data-target');
      $(target).show(1000);

    }
  });
});

try this one 试试这个

 $(document).ready(function() { $("input:radio[name='novecislo']").change(function() { var val = $(this).val(); if (val == 'Ano1') { $('#f4').show(); $('#q2').hide(); } else { $('#f4').hide(); $('#q2').show(); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="q1" class="none"> <h3>Žádáte o nové číslo?</h3> <input type="radio" name="novecislo" value="Ano1" id="Ano1"><label for="Ano1" data-target="#f4">Ano</label> <br> <input type="radio" name="novecislo" value="Ne1" id="Ne1"><label for="Ne1" data-target="#q2">Ne</label> </div> <div id="q2" class="none"> <h3>Chcete převést číslo v rámci operátora T-mobile?</h3> <input type="radio" name="op" id="Ano2"><label for="Ano2" data-target="#q3">Ano</label> <br> <input type="radio" name="op" id="Ne2"><label for="Ne2" data-target="#f1,#f4">Ne</label> <br> </div> <div id="f4" class="none"> <h4>Jaký tarif chcete zvolit?</h4> <input type="radio" name="tarif" value="tarif1" id="t1"><label for="t1" title="15Kč | bez dat | volání a sms 0,29/min">Profi na míru 1</label> <br> <input type="radio" name="tarif" value="tarif2" id="t2"><label for="t2" title="190Kč | 1,5GB | volání a sms zdarma">Profi na míru 2</label> <br> <input type="radio" name="tarif" value="tarif3" id="t3"><label for="t3" title="229Kč | 5GB | volání a sms zdarma">Profi na míru 3</label> <br> <input type="radio" name="tarif" value="tarif5" id="t5"><label for="t5" title="149Kč | bez dat | volání a sms zdarma">Profi na míru 5</label> <br> <h4>Co chcete aktivovat?</h4> <input type="checkbox" name="akt" value="roaming" id="cc1"><label for="cc1">Roaming</label> <br> <input type="checkbox" name="akt" value="platby" id="cc2"><label for="cc2">Platby</label> <p></p> <input type="button" name="send" value="ODESLAT" onclick="vypisForm();"> </div> 

Move the data-target attribute to the radio Also you cannot click and hover at the same time 将data-target属性移到收音机上您也不能同时单击和悬停

 $(function() { $('[data-target]').on("click", function() { var target = $(this).attr('data-target'); console.log(target) $(target).show(1000); }); }); 
 #f4 { display: none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="q1" class="none"> <h3>Žádáte o nové číslo?</h3> <input type="radio" name="novecislo" value="Ano1" data-target="#f4" id="Ano1"><label for="Ano1">Ano</label> <br> <input type="radio" name="novecislo" value="Ne1" data-target="#q2" id="Ne1"><label for="Ne1">Ne</label> </div> <div id="q2" class="none"> <h3>Chcete převést číslo v rámci operátora T-mobile?</h3> <input type="radio" name="op" data-target="#q3" id="Ano2"><label for="Ano2">Ano</label> <br> <input type="radio" name="op" data-target="#f1,#f4" id="Ne2"><label for="Ne2">Ne</label> <br> </div> <div id="f4" class="none"> <h4>Jaký tarif chcete zvolit?</h4> <input type="radio" name="tarif" value="tarif1" id="t1"><label for="t1" title="15Kč | bez dat | volání a sms 0,29/min">Profi na míru 1</label> <br> <input type="radio" name="tarif" value="tarif2" id="t2"><label for="t2" title="190Kč | 1,5GB | volání a sms zdarma">Profi na míru 2</label> <br> <input type="radio" name="tarif" value="tarif3" id="t3"><label for="t3" title="229Kč | 5GB | volání a sms zdarma">Profi na míru 3</label> <br> <input type="radio" name="tarif" value="tarif5" id="t5"><label for="t5" title="149Kč | bez dat | volání a sms zdarma">Profi na míru 5</label> <br> <h4>Co chcete aktivovat?</h4> <input type="checkbox" name="akt" value="roaming" id="cc1"><label for="cc1">Roaming</label> <br> <input type="checkbox" name="akt" value="platby" id="cc2"><label for="cc2">Platby</label> <p></p> <input type="button" name="send" value="ODESLAT" onclick="vypisForm();"> </div> 

First of all: use .on('click', function(){}) instead .click() 首先:使用.on('click', function(){})代替.click()

Here's why: Difference between .on('click') vs .click() 原因如下: .on('click')和.click()之间的区别

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

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