javascript - 用变量替换对象文字的属性

标签 javascript

我有一个对象文字,其中包含具有两个值(价格和名称)的国家/地区列表: enter image description here

当选择更改时,我存储当前所选国家/地区的值: enter image description here

我将这个值存储在变量内部函数.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/

相关文章:

javascript - 如何洗牌除了固定位置的数组

javascript - 使用 PhantomJS 进行单元测试时如何防止 img src 404 错误

Javascript - 在循环中动态分配 onclick 事件

Javascript CHAR 插入复制击键

javascript - 从模块中调用 Node.js 模块函数

javascript - 在 javascript 中选择特定表格元素的最聪明的方法是什么?

javascript - 使用 indexedDB 存储一个 three.js 网格对象

javascript - window.localStorage.setItem 未在 onkeydown 回调中执行

javascript - 单击 jQuery 更改 Bootstrap 指示器

javascript - Cors 似乎在我的 Node 服务器上默认启用。我错过了什么吗?