在Bootstrap 4中使用单选按钮衬里标签

时间:2017-05-26 05:11:40

标签: html css twitter-bootstrap twitter-bootstrap-4

我正在试用Bootstrap 4表单,其中我想用标签内联单选按钮。我有它使用bootstrap 3,但我似乎无法使用bootstrap 4及其自定义无线电样式。

Desired Output:
Gender : ( ) Male ( )Female

任何人都可以指出我正确的方向。 以下是我的方法

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <form class="form-horizontal" method="post">
    <div class="row">
      <div class="form-group row col-lg-6">
        <label for="firstName" class="col-3 col-form-label">First Name</label>
        <div class="col-9">
          <input type="text" class="form-control" />
        </div>
        <div class="form-check row col-lg-6">
          <label class="col-3 col-form-label">Gender</label>
          <div class="col-9">
            <label class="custom-control custom-radio">
              <input name="cat" type="radio" class="custom-control-input" value="1">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Male</span>
            </label>
            <label class="custom-control custom-radio">
              <input id="mixed0" name="cat" type="radio" class="custom-control-input" value="0">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Female</span>
            </label>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>

JS小提琴:https://jsfiddle.net/w7odmrtr/2/

2 个答案:

答案 0 :(得分:3)

我这样做:

&#13;
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
    <div class="container">
  <form class="form-horizontal" method="post">
        <div class="row">
          <div class="form-group col-lg-12">
            <label for="firstName" class="col-lg-3 col-form-label">First Name</label>
            <div class="col-lg-9">
              <input type="text" class="form-control">
            </div>
          </div>  
          <div class="form-group col-lg-12">
            <label class="custom-control custom-radio"><span class="custom-control-description">Some Label</span></label>
            <label class="custom-control custom-radio">
              <input name="cat" type="radio" class="custom-control-input" value="1">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description" th:text="#{bool.true}">Yes</span>
            </label>
            <label class="custom-control custom-radio">
              <input id="mixed0" name="cat" type="radio" class="custom-control-input" value="0">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">No</span>
            </label>
          </div>
        </div>
  </form>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

首先,div没有正确关闭,其次我只是将form-check类替换为form-group,现在它似乎正常工作。

&#13;
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <form class="form-horizontal" method="post">
    <div class="row">
      <div class="form-group row col-lg-6">
        <label for="firstName" class="col-3 col-form-label">First Name</label>
        <div class="col-9">
          <input type="text" class="form-control" />
        </div>
      </div>
      <div class="form-group row col-lg-6">
          <label class="col-3 col-form-label">Gender</label>
          <div class="col-9">
            <label class="custom-control custom-radio">
              <input name="cat" type="radio" class="custom-control-input" value="1">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Male</span>
            </label>
            <label class="custom-control custom-radio">
              <input id="mixed0" name="cat" type="radio" class="custom-control-input" value="0">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Female</span>
            </label>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>
&#13;
&#13;
&#13;