简体   繁体   中英

How to make click radio button check and uncheck

Let me explain what i want to achieve. Click on label 1 will show the button one click again i want button disable same with lable 2 and lable 3.

JsFiddle - http://jsfiddle.net/yingchor/xjzutbr9/2/

<input type='radio' class='radio-button' name='one'>
<label>label 1</label>
<input type='radio' class='radio-button' name='two'>
<label>label 2</label>
<input type='radio' class='radio-button' name='three'>
<label>label 3</label>

<div class="show-one one box" style="display: none;">
  <button>Button 1</button>
</div>
<div class="show-two two box" style="display: none;">
  <button>Button 2</button>
</div>
<div class="show-three three box" style="display: none;">
  <button>Button 3</button>
</div>


var radio_button = false;

$('input[type="radio"]').click(function() {
  var inputValue = $(this).attr("name");
  var targetBox = $("." + inputValue);
  $(".box").not(targetBox).hide();
  $(targetBox).fadeIn();
  //alert('asd')
});
$('.radio-button').on("click", function(event) {
  var this_input = $(this);
  if (this_input.attr('checked1') == '1') {
    this_input.attr('checked1', '1')
  } else {
    this_input.attr('checked1', '2')
  }

  $('.radio-button').prop('checked', false);
  if (this_input.attr('checked1') == '1') {
    this_input.prop('checked', false);
    this_input.attr('checked1', '2');
  } else {
    this_input.prop('checked', true);
    this_input.attr('checked1', '1')
  }
});

How should I change my script to achieve that?

If I understood you try this:

$('.radio-button').on("click", function(event) {
    var this_input = $(this);
    if (this_input.attr('checked1') == '1') {
        this_input.attr('checked1', '1')
    } else {
        this_input.attr('checked1', '2')
    }

    $('.radio-button').prop('checked', false);
    if (this_input.attr('checked1') == '1') {
        this_input.prop('checked', false);
        this_input.attr('checked1', '2');
        $('.box').hide(); // Hides button in second click
    } else {
        this_input.prop('checked', true);
        this_input.attr('checked1', '1')
    }

});

Here example: http://jsfiddle.net/xjzutbr9/3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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