【问题标题】:hide radio buttons on change在更改时隐藏单选按钮
【发布时间】:2017-04-17 07:47:37
【问题描述】:

我有三个单选按钮,比如 a b c,我希望如果我点击 b,下面会再看到三个单选按钮。我已经为单选按钮编写了代码,但我不知道隐藏和显示下面更改时的那些单选按钮。我试过这种方法,但无法完成

$(document).on('change','#quarterly',function(){
    $("input:radio[name='FIRST'],input:radio[name='SECOND']").hide();
});

Quarterly 是我要更改的那个按钮的 id 请帮忙!!!!! 这是html

时期 每年 季刊 月刊 时期

第一

第二
第三个

【问题讨论】:

  • 你能给我们看任何html吗?
  • @Dekel 对我不起作用
  • @DanishBhatti,你运行了这个例子吗?

标签: jquery radio-button show-hide


【解决方案1】:

我厌倦了这个,它奏效了!

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Radio Buttons</title>
<style type="text/css">
    .box{
        padding: 20px;
        display: none;
        margin-top: 20px;
        border: 1px solid #000;
    }
    .red{ background: #ff0000; }
    .green{ background: #00ff00; }
    .blue{ background: #0000ff; }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
            $("#buttonsClick").hide();
    $('input[type="radio"]').click(function(){
        if($(this).attr("value")=="red"){

            $("#buttonsClick").show();
        }
        if($(this).attr("value")=="green"){
            $(".box").not(".green").hide();
            $(".green").show();
        }
        if($(this).attr("value")=="blue"){
            $(".box").not(".blue").hide();
            $(".blue").show();
        }
    });
});
</script>
</head>
<body>
    <div>
        <label><input type="radio" name="colorRadio" value="red"> red</label>
        <label><input type="radio" name="colorRadio" value="green"> green</label>
        <label><input type="radio" name="colorRadio" value="blue"> blue</label>
    </div>
    <div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
    <div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
    <div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>
    <div id="buttonsClick">
        <input type="radio" name="colorRadio" value="red"> red
        <input type="radio" name="colorRadio" value="green"> green
        <input type="radio" name="colorRadio" value="blue"> blue
    </div>
</body>
</html>  

在加载文档时,隐藏包含示例中所示单选按钮的 div,并且元素的 onclick 函数使用 .show() 属性,工作就完成了!

【讨论】:

    【解决方案2】:

    需要注意的几点:

    1. 没有:radio css 选择器。
    2. 通常当我们使用 radio 按钮时,我们不使用 ID(而是名称),因为我们对单选按钮进行分组的方式是按其名称。
    3. 最好检查实际值(使用$(this).val())。

    这是一个工作示例:

    $(document).on('change','input[type=radio][name=opt]',function() {
      if ($(this).val() == 'opt2') {
        $("input[type='radio'][name='FIRST'],input[type='radio'][name='SECOND']").show();
      } else {
        $("input[type='radio'][name='FIRST'],input[type='radio'][name='SECOND']").hide();
      }
    });
    input[name='FIRST'], input[name='SECOND'] {
      display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input name="opt" type="radio" value="opt1" checked="checked" /> opt1 <br />
    <input name="opt" type="radio" value="opt2" /> opt2 <br />
    <input name="FIRST" type="radio" /> <br />
    <input name="SECOND" type="radio" /> <br />

    【讨论】:

    • @ImranQamer,问题中的 html 是在我的回答在这里大约一小时后发布的 :) 这个答案是通用的(没有 OP 提供的信息)。
    • 在 html 中发布的问题中没有名称为 FIRST,SECOND 的收音机,我认为您的解决方案不适用于上述情况。
    • 好的,我现在看到的 html 也不存在。
    • 我认为@Danish Bhatti 是首发
    【解决方案3】:

    使用此代码

    $(document).on('change','#quarterly',function(){
        $("#FIRST").parent(".row").hide();
        $("#SECOND").parent(".row").hide();
        $("#THIRD").parent(".row").hide();
    });
    

    $(document).on('change','#quarterly',function(){
        $("#FIRST").closest(".row").hide();
        $("#SECOND").closest(".row").hide();
        $("#THIRD").closest(".row").hide();
    });
    

    因为在您的情况下,FIRST,SECOND,THIRD 是 ID 而不是名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多