【问题标题】:How to Create radio button group to the form to allow the selection of the smoking/non-smoking section of the restaurant?如何在表单中创建单选按钮组以允许选择餐厅的吸烟/非吸烟部分?
【发布时间】:2021-10-25 07:22:45
【问题描述】:
我希望使用引导程序form-group 来模拟单选按钮的行为,因此表单中的一行有多个按钮(例如“btn btn-success”),并且可以“选择”按钮,但是在给定的时刻只能选择其中一个。
我想要这样的输出:
但我得到了这个:
我也无法选择吸烟按钮
以下代码为:
<div class="form-group row">
<label for="section" class="col-12 col-md-2">Section</label>
<div class="col-md-10">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-success active">
<input type="radio" name="options" id="option1" autocomplete="off" checked>Non-
Smoking
</label>
<label class="btn btn-danger">
<input type="radio" name="options" id="option2" autocomplete="off">Smoking
</label>
</div>
</div>
</div>
【问题讨论】:
标签:
javascript
html
css
bootstrap-4
bootstrap-modal
【解决方案1】:
将btn-group-toggle添加到您的btn-group div 将解决问题。
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
</head>
<body>
<div class="form-group row">
<label for="section" class="col-12 col-md-2">Section</label>
<div class="col-md-10">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-success active">
<input type="radio" name="options" id="option1" autocomplete="off" checked>Non-
Smoking
</label>
<label class="btn btn-danger">
<input type="radio" name="options" id="option2" autocomplete="off">Smoking
</label>
</div>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
</html>
【解决方案2】:
试试这个
input[type="radio"] {
visibility:hidden;
}