javascript - 如何附加一个函数,该函数将为一个填充下拉列表使用不同的数组?
问题描述
我有函数 getRegionsList() getLanguagesList()、两个单选按钮和一个下拉列表。问题是当我单击第二个单选按钮时,这两个列表混合在一起。我将不胜感激任何建议。[语言包含地区,但它们不应该][1]
p2.className='btn-group2';
p2.innerHTML=' <label for="contactChoice3"> <input type="radio" id="rd1" onchange="f1()" name="search" > By Region</label>'+
' <label for="contactChoice3"> <input type="radio" id="rd2" onchange="f1()" name="search" > By Language</label>';
block.appendChild(p1);
block.appendChild(p2);
let p3 = document.createElement('div');
p3.className='bottom';
p3.innerHTML=' <p>Please choose search query <select id="select" ></select></p>';
appRoot.appendChild(h1);
appRoot.appendChild(block);
appRoot.appendChild(p3);
function forEch(array){
let select =document.getElementById('select');
for (let i = 0; i < array.length; i++) {
let el = document.createElement("option"),
txt = document.createTextNode(array[i]);
el.appendChild(txt);
select.insertBefore(el, select.lastChild)
}
}
function f1(){
let a=document.getElementById('rd1');
let b=document.getElementById('rd2');
if (a.checked===true){
forEch(externalService.getRegionsList());
}else if(b.checked===true) {
forEch(externalService.getLanguagesList());
}
}
[1]: https://i.stack.imgur.com/43PUn.png
解决方案
推荐阅读
- php - 当 Laravel 用作客户端时,JWT 存储在哪里进行身份验证?
- mediawiki - 如何在 Windows 10 上为 Mediawiki 配置 Scribunto?
- mql4 - MQL4 EA 在不同图表上通过不同信号开立订单
- python - 将数组列转换为多列 Python
- bash - Jenkins mv:无法重命名:文件存在
- python-3.x - Torch 在 gcloud 上看不到带有深度学习容器的 gpu
- panoramas - 保存从一个图像链接到另一个图像的 pannellum 热点
- python - 将 dict 理解与列表理解相结合以合并 dict 对
- macos - 如何使用 Automator 通过增量编辑文本文件中的变量
- list - 检查二进制向量的字符串是否在特定索引上的元组列表中