简体   繁体   English

如何更改变量内容从循环中的数据属性获取变量名称?

[英]How to change variable content getting the variable name from a data-attribute in a loop?

I have a loop that checks the content of an element's data-attribute and then uses this content to match it in a switch function.我有一个循环来检查元素数据属性的内容,然后使用此内容在 switch 函数中匹配它。 I want to simplify this code because the data-attribute value may vary in the next future and every time I have to add a case to the switch.我想简化这段代码,因为 data-attribute 值在未来可能会有所不同,每次我必须向 switch 添加一个 case 时。 What I have now (this works fine):我现在拥有的(这很好用):

var a = $('#el').attr('data-x'); // String
for (var i = 0; i <= 2; i++) {
    switch (a) {
        case "abc":
            abc++; // variable initially set to 0
            $('#abc').text(abc); // Overwrite the content with the new value
            break;
        case "dfg":
            dfg++;
            $('#dfg').text(dfg);
            break;
[ ...Many cases like the ones listed here ]
            }
if (i === 0) { a = $('#el').attr('data-x1')} 
else if (i === 1) { a = $('#el').attr('data-x2');} 
else if (i === 2) { a = $('#el').attr('data-x3');}
}

What I want to achieve is take the 'a' value, use the value taken as a var name and then work on the variable content.我想要实现的是取 'a' 值,使用作为 var 名称的值,然后处理变量内容。

EDIT: Mostly my need is a function non-depending from the cases, the way I've used in the code above it's ok for now but every 2 months I have to add another case to make it work.编辑:主要是我需要一个不依赖于案例的函数,我在上面的代码中使用的方式现在还可以,但每两个月我必须添加另一个案例以使其工作。 The question is: how can I change my code above to make it more "universal" and "elegant" in less line of code?问题是:如何更改上面的代码以使其在更少的代码行中更加“通用”和“优雅”?

Something like:就像是:

var a = $('#el').attr('data-x');
for (var i = 0; i <= 2; i++) {
     a++; // 'a' must be the name of the var, the name of the variable it's the same as $('#el').attr('data-x').
          // So, 'a' var will change every time the function it's executed as in the switch version of the first block of code above (abc++ or dfg++).
     $('#'+a).text(a);
if (i === 0) { a = $('#el').attr('data-x1')} 
else if (i === 1) { a = $('#el').attr('data-x2');} 
else if (i === 2) { a = $('#el').attr('data-x3');}
}

Here lies my problem: 'a' will be a string ('abc' for example), abc is a global var with the number needed to be incremented and placed in $('#abc').这就是我的问题:'a' 将是一个字符串(例如'abc'),abc 是一个全局变量,它的数字需要递增并放在 $('#abc') 中。 Here we have for example:我们这里有例如:

abc = 0; (global var)
a = abc; (scoped var)
abc++; (abc === 1)
$('#abc').text(abc); (equivalent to $('#abc').text(1);)

So I need to call in the loop the 'a' value as the name of the global var, increment it, and then use 'a' as a jquery selector to text the value of the incremented global var inside it.所以我需要在循环中调用 'a' 值作为全局变量的名称,增加它,然后使用 'a' 作为 jquery 选择器来文本其中增加的全局变量的值。 How can I do that?我怎样才能做到这一点?

If I understand what you need to do, you should try to use eval()如果我明白你需要做什么,你应该尝试使用eval()

The eval() function evaluates JavaScript code represented as a string. eval() 函数计算表示为字符串的 JavaScript 代码。

var a = $('#el').attr('data-x');
for (var i = 0; i <= 2; i++) {
    eval(a + "++");
    $('#'+a).text(eval(a));
    if (i === 0) { a = $('#el').attr('data-x1')} 
    else if (i === 1) { a = $('#el').attr('data-x2');} 
    else if (i === 2) { a = $('#el').attr('data-x3');}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM