javascript - ShowHide contents base on radio button selection - Stack Overflow

I am trying to toggle a content DIV base on the selection of radio buttons. HTML for radio button. <

I am trying to toggle a content DIV base on the selection of radio buttons.

HTML for radio button.

<div class="row-fluid">
    <label class="radio">
      <input type="radio" name="account" id="yes" value="yes" checked>
      Yes, I have an existing account
    </label>
    <label class="radio">
      <input type="radio" name="account" id="no" value="no">
      No, I don't have an account
    </label>
</div>

Content DIV

<div id="account_contents">
    <p>This is account contents.......</p>  
</div>

This is how I tried it in jquery.

$('#no').bind('change',function(){
  $('#account_contents').fadeToggle(!$(this).is(':checked'));
  $('#account_contents').find("input").val("");
  $('#account_contents').find('select option:first').prop('selected',true);
});

But it doesn't work for me correctly. Here I want to show this content DIV only if user don't have an account.

Can anybody tell me how to fix this problem?

I am trying to toggle a content DIV base on the selection of radio buttons.

HTML for radio button.

<div class="row-fluid">
    <label class="radio">
      <input type="radio" name="account" id="yes" value="yes" checked>
      Yes, I have an existing account
    </label>
    <label class="radio">
      <input type="radio" name="account" id="no" value="no">
      No, I don't have an account
    </label>
</div>

Content DIV

<div id="account_contents">
    <p>This is account contents.......</p>  
</div>

This is how I tried it in jquery.

$('#no').bind('change',function(){
  $('#account_contents').fadeToggle(!$(this).is(':checked'));
  $('#account_contents').find("input").val("");
  $('#account_contents').find('select option:first').prop('selected',true);
});

But it doesn't work for me correctly. Here I want to show this content DIV only if user don't have an account.

Can anybody tell me how to fix this problem?

Share Improve this question edited Dec 26, 2015 at 10:33 AwkwardCoder 25.7k29 gold badges86 silver badges160 bronze badges asked Dec 26, 2015 at 8:45 user3733831user3733831 2,94610 gold badges39 silver badges73 bronze badges 2
  • I ran the code and it works well for me. account_contents fades in when you click "No". Did you include jquery? – Cedric Ipkiss Commented Dec 26, 2015 at 8:57
  • Yes, but, to toggle out I want to click again on #no. This is not the way I need, It should be toggle out when click on #yes. – user3733831 Commented Dec 26, 2015 at 9:02
Add a ment  | 

3 Answers 3

Reset to default 4

seems you need .on('change') for radio buttons not just for one of them

$('input[type="radio"][name="account"]').on('change',function(){
  var ThisIt = $(this);
  if(ThisIt.val() == "yes"){
       // when user select yes
       $('#account_contents').fadeOut();
  }else{
       // when user select no
       $('#account_contents').fadeIn();
       $('#account_contents').find("input").val("");
       $('#account_contents').find('select option:first').prop('selected',true);
  }
});

Working Example

  $(document).ready(function(){
            $('.radio input[type="radio"]').on("click", function(){
            if($('.radio input[type="radio"]:checked').val() === "yes"){
                $("#account_contents").slideDown("slow");
            }else{
                $("#account_contents").slideUp("slow");
            }
          });
     });

I'm not sure if I got you right, but this fiddle toggles #account_contents depending on which button you click:

This was how i tweaked the script:

  $('#no').bind('change',function(){
  $('#account_contents').fadeToggle(!$(this).is(':checked'));
  $('#account_contents').find("input").val("");
  $('#account_contents').find('select option:first').prop('selected',true);
});
$("#yes").bind("change", function() {
  $('#account_contents').fadeOut();
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745136642a4613233.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信