访问对象时使用元素ID作为键名

时间:2017-12-07 22:30:28

标签: javascript jquery

我有这个对象:

var prices = {
        monthly: {
            consumer: {
                pro: 99,
                threeSixty: 149,
                team: 249
            },
            business: {
                pro: 149,
                threeSixty: 199,
                team: 299
            },
            both: {
                pro: 199,
                threeSixty: 249,
                team: 349
            }
        },
        annual: {
            consumer: {
                pro: 79,
                threeSixty: 119,
                team: 209
            },
            business: {
                pro: 129,
                threeSixty: 169,
                team: 259
            },
            both: {
                pro: 169,
                threeSixty: 209,
                team: 299
            }
        }
    }

我需要能够使用HTML元素ID作为键名来访问其中的值。我根据点击次数获取这些ID。例如,我在点击功能中有这些:

var billingType = jQuery('.active-billing-type').attr('id');

var packageType = jQuery(this).attr('name');

我希望通过这样做来访问上述对象数据:

var newPrice = prices[billingType][packageType];

然后我尝试使用从对象检索的数据更新HTML元素,但它根本不起作用。我没有收到控制台错误,没有任何反应。建议?我做错了什么?

0 个答案:

没有答案
相关问题