How to create a toggle button in Bootstrap

前端 未结 10 688
自闭症患者
自闭症患者 2021-01-30 00:57

I originally created a web app in HTML, CSS and JavaScript, then was asked to create it again in Bootstrap too. I\'ve done it all fine, but I had toggle buttons in the web app t

10条回答
  •  孤城傲影
    2021-01-30 01:10

    Initial answer from 2013

    An excellent (unofficial) Bootstrap Switch is available.

    
    $("[name='my-checkbox']").bootstrapSwitch();
    

    It uses radio types or checkboxes as switches. A type attribute has been added since V.1.8.

    Source code is available on Github.

    Note from 2018

    I would not recommend to use those kind of old Switch buttons now, as they always seemed to suffer of usability issues as pointed by many people.

    Please consider having a look at modern Switches like this one from the React Component framework (not Bootstrap related, but can be integrated in Bootstrap grid and UI though).

    Other implementations exist for Angular, View or jQuery.

    import '../assets/index.less'
    import React from 'react'
    import ReactDOM from 'react-dom'
    import Switch from 'rc-switch'
    
    class Switcher extends React.Component {
      state = {
        disabled: false,
      }
    
      toggle = () => {
        this.setState({
          disabled: !this.state.disabled,
        })
      }
    
      render() {
        return (
          
    ) } } ReactDOM.render(, document.getElementById('__react-content'))

    Native Bootstrap Switches

    See ohkts11's answer below about the native Bootstrap switches.

提交回复
热议问题