首页 > 解决方案 > 如何附加一个函数,该函数将为一个填充下拉列表使用不同的数组?

问题描述

我有函数 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

标签: javascripthtmldomevents

解决方案


推荐阅读