How to make a radio button look like a toggle button

前端 未结 9 1935
鱼传尺愫
鱼传尺愫 2020-12-02 05:04

I want a group of radio buttons to look like a group of toggle buttons (but still function like radio buttons). It\'s not necessary that they look exactly like toggle button

相关标签:
9条回答
  • 2020-12-02 05:36

    I know this is an old question, but since I was just looking to do this, I thought I would post what I ended up with. Because I am using Bootstrap, I went with a Bootstrap option.

    HTML

    <div class="col-xs-12">
        <div class="form-group">
            <asp:HiddenField ID="hidType" runat="server" />
            <div class="btn-group" role="group" aria-label="Selection type" id="divType">
                <button type="button" class="btn btn-default BtnType" data-value="1">Food</button>                        
                <button type="button" class="btn btn-default BtnType" data-value="2">Drink</button>
            </div>
        </div>
    </div>
    

    jQuery

    $(document).ready(function () {
        $('#divType button').click(function () {
            $(this).addClass('active').siblings().removeClass('active');
            $('#<%= hidType.ClientID%>').val($(this).data('value'));
            //alert($(this).data('value'));             
        });
    });
    

    I chose to store the value in a hidden field so that it would be easy for me to get the value server-side.

    0 讨论(0)
  • 2020-12-02 05:38

    $(document).ready(function () {
        $('#divType button').click(function () {
            $(this).addClass('active').siblings().removeClass('active');
            $('#<%= hidType.ClientID%>').val($(this).data('value'));
            //alert($(this).data('value'));             
        });
    });
    <div class="col-xs-12">
        <div class="form-group">
            <asp:HiddenField ID="hidType" runat="server" />
            <div class="btn-group" role="group" aria-label="Selection type" id="divType">
                <button type="button" class="btn btn-default BtnType" data-value="1">Food</button>                        
                <button type="button" class="btn btn-default BtnType" data-value="2">Drink</button>
            </div>
        </div>
    </div>

    0 讨论(0)
  • 2020-12-02 05:46

    HTML:

    <div>
        <label> <input type="radio" name="toggle"> On </label>
        <label> Off <input type="radio" name="toggle"> </label>
    </div>
    

    CSS:

    div { overflow:auto; border:1px solid #ccc; width:100px; }
    label { float:left; padding:3px 0; width:50px; text-align:center; }
    input { vertical-align:-2px; }
    

    Live demo: http://jsfiddle.net/scymE/1/

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