51

I am trying to use the Twitter Bootstrap button group as an actual set of form input controls. By default, these button groups can be made to function like a radio button or checkbox group, but since they use the <button> element, they cannot actually be used like a radio button or checkbox.

In my research, I found this site which uses CSS to make these bootstrap buttons actually control radio buttons and checkboxes. The only issue is they use rather recent features of CSS to work, and therefore, require IE9 or above to work.

I would like to extend support to IE8. Is there another (perhaps JS controlled) solution which would offer the same features as the above link without the steep CSS requirements?

Thank you for your time.

4

6 回答 6

142

Bootstrap 3 has a "native" solution...

There now is a "true" Bootstrap solution for this problem, which appears to work fine also on older browsers. Here's what it looks like:

// get selection
$('.colors input[type=radio]').on('change', function() {
    console.log(this.value);
});
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="btn-group colors" data-toggle="buttons">
  <label class="btn btn-primary active">
    <input type="radio" name="options" value="red" autocomplete="off" checked> Red
  </label>
  <label class="btn btn-primary">
    <input type="radio" name="options" value="orange" autocomplete="off"> Orange
  </label>
  <label class="btn btn-primary">
    <input type="radio" name="options" value="yellow" autocomplete="off"> Yellow
  </label>
</div>

<!-- jQuery and Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

See the relevant Bootstrap documentation for more information.

Bootstrap 4

Bootstrap 4 supports component the same way as Bootstrap 3, but Bootstrap 4 does not support IE9. You might want to check out the Bootstrap IE8 project.

于 2013-09-18T23:49:06.073 回答
23

Bootstrap 2

Try this fiddle

HTML:

<div class="btn-group" data-toggle="buttons-radio">
    <button type="button" class="btn btn-primary">Left</button>
    <button type="button" class="btn btn-primary">Middle</button>
    <button type="button" class="btn btn-primary">Right</button>
</div>
<input type="hidden" id="buttonvalue"/>

Script:

$(".btn-group button").click(function () {
    $("#buttonvalue").val($(this).text());
});

then get buttonvalue server side

于 2013-03-12T18:24:39.640 回答
4

With Bootstrap 3.2 put the hidden input in the middle of your button group container. Instead of the text content we take the value of th data-value field.

<div id="test" class="btn-group checkit" data-toggle="buttons-radio">
    <button type="button" data-value="1" class="btn btn-default active">Yes</button>
    <input type='hidden' name="testfield" value='1'>
    <button type="button" data-value="0" class="btn btn-default ">No</button>
</div>

Now insert a little javascript snippet into the onload part of your template.

$('.checkit .btn').click(function() {
    $(this).parent().find('input').val($(this).data("value"));
});

So you only need to add .checkit to your button group and insert a hidden input field.

With bootstrap 3.2 you can use button groups directly with radio- or checkbox-inputs

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-default">
        <input type="radio" name="options" id="option1" value="1" /> Yes
    </label>
    <label class="btn btn-default">
        <input type="radio" name="options" id="option2" value="0" /> No
    </label>
    <label class="btn btn-default">
        <input type="radio" name="options" id="option3" value="42" /> Whatever
    </label>
</div>

See here: http://jsfiddle.net/DHoeschen/gmxr45hh/1/

于 2014-08-25T17:26:46.893 回答
1

You can use hidden form elements and javascript to use the button state to trigger the form element states.

于 2013-03-12T15:26:55.090 回答
1

CSS-only solution:

HTML:

<div class="btn-group">
    <label class="btn btn-primary"><input type="checkbox"><span>Button 1</span></label>
    <label class="btn btn-primary"><input type="checkbox"><span>Button 2</span></label>
</div>

SCSS/LESS:

 label.btn {
    margin-bottom: 0;
    padding: 0;
    overflow: hidden;

    span {
      display: block;
      padding: 10px 15px;
    }

    input[type=checkbox] {
      display: none;
    }

    input:checked + span {
      display: block;
      color: #fff;
      background-color: #285e8e;
    }
  }

JSfiddle here

于 2016-10-21T19:03:54.527 回答
0

If you don't want to add JS code you can use this pure CSS solution:

HTML:

<div class="btn-group colors">
  <label class="btn btn-primary">
    <input type="radio" name="g1" value="red" autocomplete="off" checked> 
    <span class='active'></span>
    <span class='label'>RED</span>
    <span></span>
  </label>

  <label class="btn btn-primary">
    <input type="radio" name="g1" value="orange" autocomplete="off"> 
    <span class='active'></span>
    <span class='label'>ORANGE</span>
  </label>

  <label class="btn btn-primary">
    <input type="radio" name="g1" value="yellow" autocomplete="off"> 
    <span class='active'></span>
    <span class='label'>YELLOW</span>
  </label>
</div>

CSS:

input {
  display:none;
}

input:checked + .active{
    background-color: #ff0000;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index:10;
}

.label{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 20;
}

You may need to set a height and width for the buttons in CSS since the spans are positioned absolute.

See here JSFiddle example

于 2021-12-17T08:57:29.730 回答