Html 使用 Bootstrap 选择表单中的多个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25701231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:39:36  来源:igfitidea点击:

Selecting multiple buttons in a form using Bootstrap

htmlformstwitter-bootstrapbutton

提问by Mobaz

I have a set of buttons that form a selection as part of my form.

我有一组按钮,它们构成了我表单的一部分。

The code is below:

代码如下:

<div class="form-group">
    <div>
        <h4>Extras</h4>
    </div>
    <div class="radio-inline">               
         <input type="button" class="btn btn-default inline" name="extra" value="Garden" > 
         <input type="button" class="btn btn-default inline" name="extra" value="Balcony">  
         <input type="button" class="btn btn-default inline" name="extra" value="Parking">  
    </div>  
</div>

However, I want to make the options not mutually exclusive i.e. you can select both 'Garden' and 'Balcony' - and I'd like to retain the button styling.

但是,我想让选项不相互排斥,即您可以同时选择“花园”和“阳台”——我想保留按钮样式。

Can anyone help with this?

有人能帮忙吗?

EDIT: Ok - got something weird going on now - Got this code.

编辑:好的 - 现在发生了一些奇怪的事情 - 得到了这个代码。

<div class="btn-group" data-toggle="buttons">
                      <label class="btn btn-default">
                        <input type="checkbox" name="garden" checked=""> Garden
                      </label>
                      <label class="btn btn-default">
                        <input type="checkbox"> Balcony
                      </label>
                      <label class="btn btn-default">
                        <input type="checkbox"> Parking
                      </label>
                    </div>

When I use bootply the multiple selection user feature works great - when I add it to my page - its not working - does anyone have any idea why it may be? Below is the whole page

当我使用 bootply 时,多选用户功能很好用 - 当我将它添加到我的页面时 - 它不起作用 - 有没有人知道为什么会这样?下面是整个页面

<div class="container">
    <div class="row">
        <div class="col-md-12"><h2>This is the header</h2></div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <h2>Hello world!</h2>
            <form d="multiform" role="form" action="listing.php" method="post" enctype="multipart/form-data">
                <div><h4>Listing Type</h4></div>

                <div class="radio">                          
                     <input type="button" class="btn btn-default" value="For Sale" > 
                     <input type="button" class="btn btn-default inline" name="list_type" value="For Rent">  
                     <input type="button" class="btn btn-default inline" name="list_type" value="Flat Share">  
                </div>                      
                <div class="form-group">
                    <label for="text">Title</label>
                        <input class= "form-control" type="text" name="text">
                </div>

                <div class="form-group">
                <label for="desc">Description</label>
                <textarea class= "form-control" name="desc" rows="5"></textarea>
                </div>


        <!nested columns in the first one!>
            <div class="col-md-6"> 

                     <div class="form-group">
                     <label for="total_beds">Total Bedrooms</label>
                         <select class="form-control" name="total_beds">
                              <option>Total Bedrooms</option>
                              <option>1</option>
                              <option>2</option>
                              <option>3</option>
                              <option>4</option>
                              <option>5</option>
                          </select>
                      </div>


                         <div class="form-group">
                     <label for="pets">Pets</label>
                         <select class="form-control" name="pets">
                              <option>Pets OK?</option>
                              <option>Yes</option>
                              <option>No</option>

                          </select>
                      </div>


                    <p>This is another p</p>
            </div>
            <div class="col-md-6">
                        <div class="form-group">
                        <label for="postcode">Postcode (for Maps)</label>
                        <input type="text" class="form-control" name="postcode" placeholder="Post Code">
                </div>
            </div>
            <div class="col-md-12">

                <div class="form-group">
                    <div><h4>Extras</h4>
                        <div class="checkbox">               
                             <input type="button" class="btn btn-default inline" name="extra" value="Garden" > 
                             <input type="button" class="btn btn-default inline" name="extra" value="Balcony">  
                             <input type="button" class="btn btn-default inline" name="extra" value="Parking">  
                    </div>  
                </div>

                    <div class="btn-group" data-toggle="buttons">
                      <label class="btn btn-default">
                        <input type="checkbox" name="garden" checked=""> Garden
                      </label>
                      <label class="btn btn-default">
                        <input type="checkbox"> Balcony
                      </label>
                      <label class="btn btn-default">
                        <input type="checkbox"> Parking
                      </label>
                    </div>
            </div>
            </form>



        </div>

        <div class="col-md-4">Hello world Right!        </div>


</div><! closes container!>

回答by rybo111

See the JavaScript buttons section of the Bootstrap documentation, and look at checkboxbuttons:

请参阅Bootstrap 文档JavaScript 按钮部分,并查看checkbox按钮:

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary active">
    <input type="checkbox" checked> Option 1 (pre-checked)
  </label>
  <label class="btn btn-primary">
    <input type="checkbox"> Option 2
  </label>
  <label class="btn btn-primary">
    <input type="checkbox"> Option 3
  </label>
</div>