Bootstrap: set initial radio button checked in HTML

后端 未结 3 478
故里飘歌
故里飘歌 2021-01-01 14:22

I\'m using Bootstrap\'s JavaScript buttons to style some radio buttons, and I don\'t seem to be able to set an initial button as checked.

Here is my code:

相关标签:
3条回答
  • 2021-01-01 14:48

    Like Skelly mentioned, the active class is what Bootstrap is toggling, it doesn't look at the checked attribute. You don't have to do it with Javascript however...

    <label class="btn btn-info active">
        <input checked type="radio" id="match-three" name="options">Three numbers
    </label>
    

    Will give you what you're looking for.

    0 讨论(0)
  • 2021-01-01 14:54

    If you're using the HTML 5 doctype, just use checked without any value:

    <label class="btn btn-info active">
        <input checked type="radio" id="match-three" name="options">Three numbers
    </label>
    

    checked="checked" should work as well, so you'll have to clarify what's not working for you.

    • Having a checked attribute on the input will ensure correct state on the form field
    • Adding an active class on the label will set the correct visual state

    Reference documentation.

    0 讨论(0)
  • 2021-01-01 14:58

    If you want the Bootstrap button() component to recognize the checked inputs initially you need to add some jQuery like..

    $('[checked="checked"]').parent().addClass("active");
    

    This will set the active state to the appropriate buttons/labels in the group.

    0 讨论(0)
提交回复
热议问题