【问题标题】:Default radio button option with jqueryjquery的默认单选按钮选项
【发布时间】:2022-02-09 09:35:30
【问题描述】:

我正在尝试使用here 显示的方法隐藏/显示不同的 div。

<style>
    .box{
        color: #fff;
        padding: 20px;
        display: none;
        margin-top: 20px;
    }
    .red{ background: #ff0000; }
    .green{ background: #228B22; }
    .blue{ background: #0000ff; }
    label{ margin-right: 15px; }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $('input[type="radio"]').click(function(){
        var inputValue = $(this).attr("value");
        var targetBox = $("." + inputValue);
        $(".box").not(targetBox).hide();
        $(targetBox).show();
    });
});
</script>
</head>
<body>
    <div>
        <label><input type="radio" name="colorRadio" value="red" checked = "checked"> 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>
</body>

我希望在页面加载时默认点击红色按钮。但是,如果我只是将标签更改为:

<label><input type="radio" name="colorRadio" value="red" checked = "checked"> red</label>

它显示为点击:



但我必须再次单击它才能实际加载红色 div:



有没有一种优雅的方法来解决这个问题,以便在页面加载时显示红色 div?

非常感谢, 亚历克斯

【问题讨论】:

  • 在事件处理程序结束后添加.click()。例如。 $('input[type="radio"]').click(function(){...).click()
  • 感谢 j08691,我无法使用此解决方案。可能是我在做一些愚蠢的事情,但是你的解决方案没有修改就可以工作,所以我就这样做了。
  • 实际上,最后的 .click() 会选择最后一个输入。但是在点击处理程序之后添加$("input:checked").click() 就可以了

标签: javascript html jquery


【解决方案1】:

添加$('.red').show() 来设置默认显示哪个框怎么样?

$(document).ready(function () {
  $('input[type="radio"]').click(function () {
    var inputValue = $(this).attr('value');
    var targetBox = $('.' + inputValue);
    $('.box').not(targetBox).hide();
    $(targetBox).show();
  });

  $('.red').show();
});
.box {
  color: #fff;
  padding: 20px;
  display: none;
  margin-top: 20px;
}
.red {
  background: #ff0000;
}
.green {
  background: #228b22;
}
.blue {
  background: #0000ff;
}
label {
  margin-right: 15px;
}
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

<body>
    <div>
        <label><input type="radio" name="colorRadio" value="red" checked> 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>
</body>

【讨论】:

  • 谢谢!这有效!
猜你喜欢
  • 1970-01-01
  • 2018-12-26
  • 1970-01-01
  • 2017-10-02
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
  • 2014-03-08
相关资源
最近更新 更多