我有一个对象文字,其中包含具有两个值(价格和名称)的国家/地区列表:
当选择更改时,我存储当前所选国家/地区的值:
我将这个值存储在变量内部函数.change()中:
current_country = $('#country').val();
我以这种方式访问对象文字,例如countries_list.deu
但是当我使用 current_country 变量控制台时会显示未定义的内容:
console.log(countries_list.current_country);
我该如何解决这个问题?
最佳答案
使用selectedIndex获取所选值索引,使用.options[i].value获取索引如下的选项的值:
var countries = document.getElementById("country");
var current_country = countries.options[countries.selectedIndex].value;
关于javascript - 用变量替换对象文字的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28278250/