简体   繁体   English

如何使用下拉列表对单选按钮进行逻辑分组

[英]How to logically group a radio button with a drop down list

 function radioCheck() { document.getElementById("last").checked = "checked"; }
 <label> <input type="radio" name="Ppub" value="" checked="checked">All Dates </label> <br> <label> <input type="radio" id="last" name="Ppub" value=""> Last </label> <select name="Ppub" id="selectRange" value="" onchange="radioCheck();"> <option value="">Select</option> <option value="20181220-20190120">month</option> <option value="20180720-20190120">6 months</option> <option value="20180120-20190120">year</option> </select>

I am working on a search form and I have a group of radio buttons to filter the search.我正在处理一个搜索表单,我有一组单选按钮来过滤搜索。 For one of the radio buttons, I want it to be "linked" to a drop down menu.对于其中一个单选按钮,我希望将其“链接”到下拉菜单。

I am able to make the status of the radio button "checked" when the user selects an item from the menu.当用户从菜单中选择一个项目时,我能够使单选按钮的状态“被选中”。 However, if the user checks another radio button, the value of the selected item is still used.但是,如果用户选中另一个单选按钮,则仍会使用所选项目的值。

How do I group the radio button and drop down menu?如何对单选按钮和下拉菜单进行分组? I tried putting them in the same label but to no avail.我尝试将它们放在同一个标​​签中,但无济于事。

You have to reset the drop down on clicking the radio button.您必须在单击单选按钮时重置下拉菜单。 Also I can not find any reason to the name of the drop down as it is the same of the radio buttons.我也找不到下拉菜单名称的任何理由,因为它与单选按钮相同。

Please Notice the changes in the value attribute changes of the radio buttons.请注意单选按钮的value属性更改的更改。

 var radio = document.querySelector('[value=all]'); radio.onclick = function(){ document.getElementById("selectRange").selectedIndex = 0; } function radioCheck() { document.getElementById("last").checked = "checked"; }
 <label> <input type="radio" name="Ppub" value="all" checked="checked">All Dates </label> <br> <label> <input type="radio" id="last" name="Ppub" value="last"> Last </label> <select id="selectRange" value="" onchange="radioCheck();"> <option value="">Select</option> <option value="20181220-20190120">month</option> <option value="20180720-20190120">6 months</option> <option value="20180120-20190120">year</option> </select>

May be the following can help you to achieve what you are looking for (Plesae notice the changes in the HTML, the wrapping div with each group of radio and select).可能以下内容可以帮助您实现您正在寻找的内容(请注意HTML中的变化,每组radio和select的包装div)。

 var radioAll = document.querySelectorAll('[type=radio]'); radioAll.forEach(function(r){ r.onclick = function(){ var ddAll = document.querySelectorAll('select'); ddAll.forEach(function(s){ s.selectedIndex = 0; }); } }); function radioCheck(el) { var input = el.parentNode.querySelector('[name=Ppub]'); input.checked = "checked"; }
 <label> <input type="radio" name="Ppub" value="all" checked="checked">All Dates </label> <br> <div> <label> <input type="radio" id="last" name="Ppub" value="last"> Last </label> <select id="selectRange" value="" onchange="radioCheck(this);"> <option value="">Select</option> <option value="20181220-20190120">month</option> <option value="20180720-20190120">6 months</option> <option value="20180120-20190120">year</option> </select> </div> <div> <label> <input type="radio" name="Ppub" value=""> Cutom range </label> <select value="" onchange="radioCheck(this);"> <option value="">Select</option> <option value="20181220-20190120">Test1</option> <option value="20180720-20190120">Test2</option> <option value="20180120-20190120">Test3</option> </select> </div>

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

相关问题 如何根据选择的单选按钮更改下拉列表的值? - How to change value of a drop down list depend on selected radio button? 如何将与单选按钮分组的下拉列表值存储到数据库 - how to store a drop down list value grouped with radio button to database HTML下拉列表选择单选按钮 - HTML Drop Down List to Select Radio Button 选择单选按钮会创建一个动态下拉列表 - Radio button selection creates a dynamic drop down list 计算单选按钮和下拉列表的价格 - Calculate price of radio button and drop-down list 单选按钮的更改顺序:在下拉列表上方显示选中的选项 - Change order of radio button: display the checked above the drop down list 如何在JavaScript中选择单选按钮时根据下拉列表在文本框中显示相应的值? - How to display the corresponding values in the text box depending on drop down list on selecting radio button in javascript? 如何在下拉自定义列表单选按钮中添加两个以上的选项? - How to add more than two options in a drop down custom list radio button? jQuery-如何获取单选按钮/复选框/列表/下拉菜单中屏幕上显示的文本 - jquery- how to obtain the text shown on screen in a radio button/check box/list/drop down 单选按钮如何改变<option value="of a drop-down list in JS?">JS中的下拉列表?</option> - How can a radio button change an <option> of a drop-down list in JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM