简体   繁体   English

CSS:按下与单选按钮分组的下拉菜单

[英]CSS: Depress Dropdown grouped with Radio Buttons

I'm using bootstrap to style a group of radio buttons that also include a dropdown.我正在使用引导程序来设计一组还包括下拉列表的单选按钮。

When the user selects an option from the dropdown OTHER is not included as one of the radio buttons, so it looks like the last depressed radio button has been chosen when really C | D | E当用户从下拉列表中选择一个选项时OTHER不作为单选按钮之一包含在内,所以看起来最后按下的单选按钮已经被选中,当真的C | D | E C | D | E C | D | E has been. C | D | E已经。

在此处输入图片说明

Is it possible to include the OTHER dropdown as one of the radio buttons so it looks depressed when the user selected C,D,E?是否可以将 OTHER 下拉列表作为单选按钮之一包含在内,以便在用户选择 C、D、E 时看起来很沮丧?

Codepen here if helpful: https://codepen.io/mayagans/pen/qBdaZEJ如果有帮助,在此处使用 Codepen: https ://codepen.io/mayagans/pen/qBdaZEJ

 $( document ).ready(function() { $("input[name='test']").change(function(){ $("#results").text($("input[name='test']:checked").val()); }); }); $(".dropdownitems").click(function () { var value = $(this).attr("href"); document.getElementById("results").innerHTML = value });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="btn-group" data-toggle="buttons"> <label id='RADIO' class="btn btn-primary"> <input type="radio" name="test" id="testNONE" autocomplete="off" value="NONE">NONE </label> <label class="btn btn-primary"> <input type="radio" name="test" id="testA" autocomplete="off" value="A">A </label> <label class="btn btn-primary"> <input type="radio" name="test" id="testB" autocomplete="off" value="B">B </label> <div class="btn-group"> <label class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <input type="radio" class="" name="test" id="OTHER" autocomplete="off">OTHER<span class="caret"></span> </label> <ul class="dropdown-menu"> <li><a href="C" class="dropdownitems" id="C">C</a></li> <li><a href="D" class="dropdownitems" id="D">D</a></li> <li><a href="E" class="dropdownitems" id="E">E</a></li> </ul> </div> </div> <div id="results"></div>

I don't think that radio buttons are the good solution for this.我不认为单选按钮是解决这个问题的好方法。 You could just use "simple" buttons.您可以只使用“简单”按钮。 Anyway, with bootstrap, you could add "active" to the button ( or radio button) class so that it will be shown as if it was pressed.无论如何,使用引导程序,您可以将“活动”添加到按钮(或单选按钮)类中,以便它显示为被按下。 With single button带单按钮

Pressed按下

 <button type="button" class="btn btn-primary active">Click Me!</button>

Normal普通的

 <button type="button" class="btn btn-primary">Click Me!</button>

With your radio button it's the same用你的单选按钮是一样的

Pressed按下

<label class='btn btn-primary active'>
    <input type='radio' name='test' id='testA' autocomplete='off' value='A'>A
  </label>

Normal普通的

<label class='btn btn-primary'>
    <input type='radio' name='test' id='testA' autocomplete='off' value='A'>A
  </label>

Example例子

<script>
    $(document).ready(function () {
        let pressedId
        let buttonsId = ["a", "b"] // List of the buttons which are always visible
        let dropDownsButtonId = ["c", "d", "e"] //List of the buttons of the dropdown
        $("button").click(function () {
            if (buttonsId.includes(this.id)) { //First "if" for "always visible" button
                changeClass(this.id)
            } else if (dropDownsButtonId.includes(this.id)) {//Second if for the button of the dropdown
                changeClass("dropDownMenuButton")
            }
        })

        function changeClass(id) {
            if (pressedId !== undefined)
                document.getElementById(pressedId).className = document.getElementById(pressedId).className.replace(" active", "")
            document.getElementById(id).className += " active"
            pressedId = id
        }
    })
</script>

<button type="button" class="btn btn-primary" id="a">a</button>
    <button type="button" class="btn btn-primary" id="b">b</button>
    <div class="dropdown">
        <div class='btn-group'>
            <button class="btn btn-primary dropdown-toggle" type="button" id="dropDownMenuButton"
                    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Dropdown button
            </button>
            <ul class='dropdown-menu'>
                <li>
                    <button type="button" class="btn btn-primary" id="c">c</button>
                </li>
                <li>
                    <button type="button" class="btn btn-primary" id="d">d</button>
                </li>
                <li>
                    <button type="button" class="btn btn-primary" id="e">e</button>
                </li>
            </ul>
        </div>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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