如何为表单创建单选按钮组以允许选择餐厅的 smoking/non-smoking 部分?

How to Create radio button group to the form to allow the selection of the smoking/non-smoking section of the restaurant?

我想用 bootstrap form-group 模拟单选按钮的行为,所以表单中的一行有几个按钮(例如“btn btn-success”),按钮是能够被“selected”,但在给定时刻只能selected 其中之一。 我想要这样的输出:

但我得到这个:

我也无法 select 吸烟按钮

以下代码是:

 <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>

试试这个

input[type="radio"] {
    visibility:hidden;
}

https://jsfiddle.net/auo9Ly54/

删除 autocomplete="off" checked in:

<input type="radio" name="options" id="option1" autocomplete="off" checked>

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>