Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript创建选项元素并将其设置为';s值_Javascript_Arrays_Function - Fatal编程技术网

如何使用javascript创建选项元素并将其设置为';s值

如何使用javascript创建选项元素并将其设置为';s值,javascript,arrays,function,Javascript,Arrays,Function,我的代码有问题。我想使用javascript创建一个option元素。我在下面做过类似的事情。我的问题是什么 我希望货币数组中的每个项目都填充到PopulateCurrences函数中。它还应该创建一个option元素。并将其值设置为项目id const currencies = [ { id: 'USD', name: 'US Dollars' }, { id: 'UGX', name: 'Ugandan Shillings' }, { id: 'KES',

我的代码有问题。我想使用javascript创建一个option元素。我在下面做过类似的事情。我的问题是什么

我希望货币数组中的每个项目都填充到PopulateCurrences函数中。它还应该创建一个option元素。并将其值设置为项目id

const currencies = [
  {
    id: 'USD', name: 'US Dollars'
  }, {
    id: 'UGX', name: 'Ugandan Shillings'
  }, {
    id: 'KES', name: 'Kenyan Shillings'
  }, {
    id: 'GHS', name: 'Ghanian Cedi'
  }, {
    id: 'ZAR', name: 'South African Rand'
  }
];

它与DOM操作有关

const populateCurrencies = () => {  

            var ans = document.getElementById('USD').value;
}

您需要的输出是什么?我需要用户单击某个选项时的输出以及用户所选内容的值应显示在浏览器中。和的可能重复
currencies.forEach((x)=>{
let selectElt = document.querySelector('.select-text');
let newElt = document.createElement('option');
let newText = document.createTextNode(x.name);
newElt.setAttribute('value', 'x.id');
newElt.appendChild(newText);
selectElt.appendChild(newElt);
})