jq发送动态变量_jQuery的动态变量名

I would like to take the value of an li ID attribute (which will be a userID), and use it as part of a string that I will eventually use as part of a variable name. I will use this variable name to create an array with.

I understand the basics, but can't seem to find the right combination of jQuery/javascript to make this magic happen.

jQuery('#user-list li').click(function() {

var userID = jQuery(this).attr("id");

// i want to add the word array to the end of userID

var theVariableName = userID + "Array";

// I want to use this variable to create an array

var theVariableName = new Array();

// I want to continue to use the name throughout my document

theVariableName.push({startTime: 7, endTime: 10});

alert(theVariableName[0].startTime);

});

解决方案

Use an Object to hold the various user arrays:

window.userData = {};

$(...).click(function() {

// ...

window.userData[userID] = [];

window.userData[userID].push({startTime:7, endTime:10});

alert(window.userData[userID][0].startTime);

}

You might not want to store the userData object in the global namespace though; to prevent accidental name conflicts, you should at least put it in your own namespace.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值