简体   繁体   中英

Bootstrap uncheck checkbox and style

I have 3 Buttons / checkboxes, if I click on any it gets checked, how do I make all other Buttons uncheck if I click on the current one.

https://jsfiddle.net/DTcHh/31573/

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-default btn-lg no-border">
        <span id="btnPencil" class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
        <input type="checkbox" autocomplete="off">
    </label>
    <label class="btn btn-default btn-lg no-border">
        <span class="glyphicon glyphicon-th-large" aria-hidden="true"></span>
        <input type="checkbox" autocomplete="off">
    </label>
    <label class="btn btn-default btn-lg no-border">
        <span class="glyphicon glyphicon-th-large" aria-hidden="true"></span>
        <input type="checkbox" autocomplete="off">
    </label>
</div>

btnp = document.getElementById(btnPencil)

btnp.addEventListener('change', function() {
    // 1. check current button / checkbox
    // 2. uncheck other buttons / checkbox
    // bootstrap uncheck / check style ?
});

You should really use radio buttons for this, not checkboxes, and style them to look like checkboxes (if you prefer that look). So instead of

<input type="checkbox" autocomplete="off">

Just use

<input type="radio" autocomplete="off" name="X">

And make sure all radios have the same name attribute.

 /* Latest compiled and minified JavaScript included as External Resource */ 
 /* Latest compiled and minified CSS included as External Resource*/ /* Optional theme */ @import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css'); body { margin: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default btn-lg no-border"> <span id="btnPencil" class="glyphicon glyphicon-pencil" aria-hidden="true"></span> <input id="checkboxPencil" type="radio" autocomplete="off" name="menu"> </label> <label class="btn btn-default btn-lg no-border"> <span id="btnSquare" class="glyphicon glyphicon-th-large" aria-hidden="true"></span> <input id="checkboxSquare" type="radio" autocomplete="off" name="menu"> </label> <label class="btn btn-default btn-lg no-border"> <span id="btnSquare" class="glyphicon glyphicon-th-large" aria-hidden="true"></span> <input id="checkboxSquare2" type="radio" autocomplete="off" name="menu"> </label> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM