jq发送动态变量_jQuery动态变量名(由字符串和数字组成)

这篇博客探讨了如何在jQuery中动态访问JSON对象,特别是当变量名由字符串和数字组成时。作者遇到了在Ajax GET请求成功后,尝试通过循环将响应数据追加到HTML代码时的问题。解决方案是使用JSON对象的访问语法`resp[name].d0`,以及使用`$.each`遍历响应数据,简化并优化代码结构。
摘要由CSDN通过智能技术生成

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

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.

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 = size of resp

for example resp.sp0 = data item

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

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

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

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

etc..

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);

THE COMPLETE CODE CODE :

//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.

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.

解决方案

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.

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);

});

}

});

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" }

}

}

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

// ....

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值