开发者

Accessing Variables in Javascript using .length

开发者 https://www.devze.com 2023-01-03 14:33 出处:网络
I\'m pretty new to Javascript, so forgive me if this is a simple question. I\'m trying to access the length of a set of checkboxes in a form using Javascript. However, I need to be able to change the

I'm pretty new to Javascript, so forgive me if this is a simple question.

I'm trying to access the length of a set of checkboxes in a form using Javascript. However, I need to be able to change the "name" field of the checkboxes to check several different sets of them. Right now, my sample开发者_如何学Python code looks like:

var set = "set" + x;
totalLength = optionBoxes.set.length;

The variable x is being incremented by a for loop that wraps the whole thing and the name of the checkbox sets that I'm trying to access are set0, set1, set2, etc.

Thanks.

Edit: small typo fixes


Probably you want this:

var set = "set" + x;
totalLength = optionBoxes[set].length;

In Javascript, properties of an object are usually accessed as object.name, but they can also be accessed by object["name"] if you have the name as a string.


if you think that your code should otherwise work try:

totalLength = optionBoxes[set].length;

0

精彩评论

暂无评论...
验证码 换一张
取 消