簡體   English   中英

如何使單擊單選按鈕選中和取消選中

[英]How to make click radio button check and uncheck

讓我解釋一下我想要實現的目標。 單擊標簽1將再次顯示按鈕一次,我希望按鈕禁用與標簽2和標簽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')
  }
});

我應該如何更改腳本以實現該目標?

如果我了解您的嘗試,請執行以下操作:

$('.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')
    }

});

例如: http : //jsfiddle.net/xjzutbr9/3/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM