[英]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.