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(n

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值