我有两个单选按钮和一个在我的网站上设置了一个选项的选项,乍一看在我看来这很容易,但我已经坐了一个半小时,无法弄清楚如何实施它。
I need to remove specific options from select depending on the selected radio button, but in such a way that when another radio button is selected, return the removed options by removing others, i.e.:
这里有两个单选按钮 -radio1和radio2, 和一个下拉菜单select- option1,option2和option3,
当我选择radio1, 然后 和 被删除option1,option2当我选择radio2, 然后option3, BUT option1和被option2返回。
remove()通过、NOT 删除是必要的display: none;,而且<select>应该只有一个 - 这就是整个困难!
请帮忙。
let options = [...document.querySelector("select").options];
document.querySelectorAll("input[name=sel]").forEach(function (current_radio) {
current_radio.addEventListener("change", function () {
if (current_radio.value === "one") {
} else {
}
});
});
<input type="radio" name="sel" value="one" />
<input type="radio" name="sel" value="two" />
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
你可以尝试一次得到所有东西
options,然后和他们一起工作使用 remove() 方法。在标记中,为您的选择提供一个 id:
在脚本中:
自己适应你的情况。
简而言之: