首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使点击单选按钮选中和取消选中

如何使点击单选按钮选中和取消选中
EN

Stack Overflow用户
提问于 2018-01-28 14:44:42
回答 1查看 74关注 0票数 0

让我解释一下我想要实现的目标。单击标签1将再次显示该按钮,我希望禁用与标签2和标签3相同的按钮。

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

代码语言:javascript
复制
<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')
  }
});

我应该如何改变我的脚本来实现这个目标呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-28 15:46:23

如果我理解你的意思,你可以试试这个:

代码语言:javascript
复制
$('.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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48483881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档