小编Jos*_*osh的帖子

通过变量访问属性时出现语法错误?

我有一个存储在$("#budget")中的json变量.data('allocation')

我可以像这样访问它的数据:

id = "5";
alert( $("#budget").data('allocations')[id].amount );
Run Code Online (Sandbox Code Playgroud)

但我需要像这样访问它:

var id = "5";
var field = "amount";

alert( $("#budget").data('allocations')[id].[field] );
Run Code Online (Sandbox Code Playgroud)

使用属性名称中的变量会导致失败.

之后缺少名字.运算符(指[field])

javascript properties

0
推荐指数
1
解决办法
50
查看次数

标签 统计

javascript ×1

properties ×1