Jquery对象分配给变量返回未定义[重复]

Posted

技术标签:

【中文标题】Jquery对象分配给变量返回未定义[重复]【英文标题】:Jquery object assign to variable return undefined [duplicate] 【发布时间】:2016-03-19 17:06:32 【问题描述】:

下面的工作 SO 代码:

var property_object_parse = jQuery.parseJSON('"p1":"TextElement":["yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_company name","text":"Company name","font":"Plantagenet Cherokee","size":"9","h":"0.16","w":"1.15","y":"0.3","x":"0.53","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"company name","caption":"Company name","textType":"Company name","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"left","sortorder":"0","inputformat":false,"format":false,"clippath":false,"inputrequired":false,"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_first name","text":"First name","font":"Plantagenet Cherokee","size":"7","h":"0.1","w":"1.14","y":"0.44","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"first name","caption":"First name","textType":"First name","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"1","inputformat":false,"format":false,"clippath":false,"inputrequired":false,"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_last name","text":"Last Name","font":"Plantagenet Cherokee","size":"6","h":"0.11","w":"1.16","y":"0.56","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"last name","caption":"Last Name","textType":"Last Name","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"2","inputformat":false,"format":false,"clippath":false,"inputrequired":false,"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_email","text":"Email","font":"Plantagenet Cherokee","size":"6","h":"0.1","w":"1.14","y":"0.69","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"email","caption":"Email","textType":"Email","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"3","inputformat":false,"format":false,"clippath":false,"inputrequired":false,"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_phone","text":"Phone","font":"Plantagenet Cherokee","size":"6","h":"0.11","w":"1.14","y":"0.81","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"phone","caption":"Phone","textType":"Phone","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"4","inputformat":false,"format":false,"clippath":false,"inputrequired":false,"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#1_street","text":"street","font":"Plantagenet Cherokee","size":"9","h":"0.17","w":"1.16","y":"0.95","x":"2.3","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"street","caption":"street","textType":"street","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"center","align":"right","sortorder":"5","inputformat":false,"format":false,"clippath":false,"inputrequired":false],"p2":"ImageElement":["type":"image","text":"Sample.png","blockName":"Back Logo","id":"#2_Back Logo","lock":"false","rotation":"0","h":"0.68","w":"0.69","y":"0.59","x":"0.41","valign":"bottom","halign":"left","clippath":false],"TextElement":["yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#2_Company Name","text":"Company Name","font":"Plantagenet Cherokee","size":"9","h":"0.16","w":"1.16","y":"1.3","x":"0.24","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"Company Name","caption":"Company Name","textType":"Company Name","bold":"false","colorspace":"DevicaeRGB","colorcode":"rgb(1.00,0.00,0.00)","color":"16711680","hex_color":"","valign":"bottom","align":"center","sortorder":"0","inputformat":false,"format":false,"clippath":false,"inputrequired":false,"yscale":"100","xscale":"100","shadow":"false","rotation":"","id":"#2_Tagline","text":"Sample Tagline","font":"Helvetica","size":"12","h":"0.18","w":"0.73","y":"1.52","x":"0.25","lock":"false","customize":"false","Alpha":"100","underline":"false","italic":"","blockName":"Tagline","caption":"Sample Tagline","textType":"Sample Tagline","bold":"false","colorspace":"DeviceRGB","colorcode":"rgb(1.00,1.00,1.00)","color":"16777215","hex_color":"","valign":"top","align":"left","sortorder":"1","inputformat":false,"format":false,"clippath":false,"inputrequired":false]');
var selected_element = 'p1.TextElement[0].size';
console.log(property_object_parse.selected_element);

当我尝试使用变量获取值时,我得到了undefined。这是正确的访问方式吗?

见我的jsfiddle here

【问题讨论】:

你好,不会说Welcome to SO!,因为你已经在这里很久了。而且你应该知道,在提问时,代码应该在这里添加到 SO 上。我强烈坚持看How to Ask和minimal reproducible example。 @Tushar 当用户在这里添加代码时,你们会告诉他们创建 jsfiddle 并显示,如果用户创建 fiddle,你会告诉他们编写代码。 @BhumiShah 不,小提琴中的代码是次要的,如果你添加 completerelevant 代码本身,没有人会要求你创建小提琴。 jsfiddle.net/1q3wnddg/1 检查这个。检查控制台 试试这个? jsfiddle.net/xfh8eLyp 【参考方案1】:

property_object_parse是一个真正的javascript对象,所以你可以直接使用成员访问语法来访问你感兴趣的值:

console.log(property_object_parse.p1.TextElement[0].size);

请注意,您不能使用像'p1.TextElement[0].size' 这样的动态属性路径字符串,您必须以某种方式对其进行编译。例如,您可以改为使用一组您尝试访问的属性:

var selected_element = ['p1', 'TextElement', '0', 'size'];
var obj = property_object_parse;
for (var i = 0; i < selected_element.length; i++) 
    obj = obj[selected_element[i]];

console.log(obj);

这与上面使用property_object_parse.p1.TextElement[0].size 直接访问它的结果相同。

【讨论】:

感谢戳,是的,我有两个选择:1. 使用 eval 和 2. 分成几部分,但 eval 不好用,所以我像你展示的那样分成几部分。

以上是关于Jquery对象分配给变量返回未定义[重复]的主要内容,如果未能解决你的问题,请参考以下文章

顶部未定义的环境变量

如何使用 POST 方法将 javascript/jQuery 变量传递给 PHP [重复]

使用jquery发布变量时如何修复“未定义索引”[重复]

无法在 Vue.js 中设置未定义的属性“产品”[重复]

对象属性的双重分配导致未定义的属性[重复]

VBA:无法将函数的返回值分配给变量[重复]