jQuery变量名动态地(由字符串和数字组成) [英] Jquery variablename dynamically (compose from string and number)

查看:421
本文介绍了jQuery变量名动态地(由字符串和数字组成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我非常想寻找以下问题的解决方案.

Hello I'm quite desperate searching for a solution for the following problem.

在我的脚本中,我发出一个Ajax GET请求,该请求返回JSON数据. 它成功返回了数据,此后我将其循环,然后出现了我的问题.

In my script I make an Ajax GET request which returns JSON data. It succesfully returns the data, after this I loop it and then my problem occurs.

我拥有返回的主要数据的数据大小,然后进行循环以将每个结果附加到我的html代码中.

I have the size of the data among with the main data returned and then I make a loop to append each result to my html code.

resp.size = resp的大小 例如resp.sp0 =数据项

resp.size = size of resp for example resp.sp0 = data item

对于每个步骤,然后应将其附加到主要的html代码中.

For each step it should then append to the main html code.

$(.magicproducts").append(resp.sp0.d1);

$(".magicproducts").append(resp.sp0.d1);

$(.magicproducts").append(resp.sp1.d1);

$(".magicproducts").append(resp.sp1.d1);

$(.magicproducts").append(resp.sp2.d1);

$(".magicproducts").append(resp.sp2.d1);

等.

我在循环中尝试了此操作,但是它不起作用,它会警告正确的名称,但是当我将其用作变量名时不起作用.

I tried this in my loop but it does not work, it alerts the correct name but does not work when i use it as a variablename.

var name ="resp.sp" + i; alert(name);

var name = "resp.sp"+i; alert(name);

完整的代码

//SET URL TO FOR AJAX GET

var url = $("#url").val();

//

//START AJAX CALL

$.ajax({ 
type: "GET",
dataType: 'json',
url: "../../inc/myscript.php?url="+url,
success: function(resp){
var datasize = (resp.size);
var i = 0;
do {
var name = "resp.sp"+i; 
alert(name);
$(".magicproducts").append(name.d0);
i++;
}
while (i < datasize+1);
} 
});

});

关于如何解决它,我将提供任何输入或答案,就像我现在正在处理它时一样.

I would appriciate any inputs or answers on how to solve it in the same style as im approaching it now.

第二,我想知道是否没有其他方法. 某种foreach?以便它自动循环显示resp数据包含的所有项目.

Second to this Im wondering if there is no other approach. Somesort of foreach ? So that it auto loops all the items the resp data contains.

推荐答案

使用字符串访问JSON对象的语法是:

The syntax for accessing a JSON object with a string is this:

var name = "sp"+i;
resp[name].d0

要回答第二个问题,是的,也许会有更好的方法.

To answer your second question, yes there might be a better way.

由于您已经在使用jQuery,因此可以像这样$.each:

Since you are already using jquery, you can $.each like so:

//SET URL TO FOR AJAX GET

var url = $("#url").val();

//

//START AJAX CALL

$.ajax({ 
    type: "GET",
    dataType: 'json',
    url: "../../inc/myscript.php?url="+url,
    success: function(resp){
        $.each(resp, function(key, value) {
            // key is sp0, sp1, sp2...
            // value is { d0: "magic_product", d1: "more_data", d3: "even more data", etc... }
            $('.magicproducts').append(value.d0);
        });
    } 
});

您还可以嵌套$ .each语句,以尽可能深入地探究JSON对象.假设您的JSON看起来像这样

You can also nest the $.each statements to dig into JSON object as far as you want. Let's say your JSON looks like this

{ 
    "sp0" : 
    { 
      "d0": { "hello": "world" },
      "d1": { "hello": "mom" } 
    },
    "sp1" : 
    { 
      "d0": { "hello": "dad" },
      "d1": { "hello": "earth" } 
    }
}

您可以嵌套$.each语句以遍历JSON对象的每个级别,如下所示:

You can nest your $.each statements to loop through each level of the JSON object like so:

// first foreach statement to loop through sp0, sp1, etc...
$.each(resp, function(sp, d) {
    // second foreach statement to loop through d0, d1, d2, etc....
    $.each(d, function(i, value) {
        // do something with value
        // ....
    });
});

这篇关于jQuery变量名动态地(由字符串和数字组成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆