抓取jQuery模板数组

时间:2015-06-19 10:03:22

标签: javascript jquery arrays jquery-templates

我有一个数组sizes,我可以在我的jQuery模板中访问它。但是,在某种情况下,阵列只有一个项目,我想直接访问它。如何使用jQuery模板执行此操作?

此尝试不起作用:

<input type="hidden" name="product_id" data-related-product-id="${$index}" value="${sizes[0]}${product_id}" />

并输出:

<input type="hidden" value="[object Object]{product_id}" data-related-product-id="1" name="product_id">

我似乎无法找到有关如何执行此操作的任何文档。

这是JSON.stringifysizes的输出。

  

[{&#34; PRODUCT_ID&#34;:&#34; 51507322000&#34;&#34; free_stock&#34;:&#34; 0&#34;&#34;尺寸&#34 ;: &#34; 000xs&#34;&#34;标题&#34;:&#34; XS&#34;&#34; sizes_group_id&#34;:&#34; 29,33&#34;&#34 ; oos_status&#34;:&#34; 0&#34;&#34;系&#34;:&#34; 51&#34;&#34;蛞蝓&#34;:&#34;这-是 - 的段塞&#34;&#34; sizes_group&#34; {&#34; 29&#34; {&#34; ID&#34;:&#34; 29&#34;&#34;标题& #34;:&#34; XS&#34;&#34;位置&#34;:&#34; 20&#34;&#34; type_title&#34;:&#34;服装&#34;,& #34; depts_to_include&#34;:&#34;,01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55 ,28,26,20,33,19,14,16,25,27,18,17,65,98,&#34;&#34; depts_to_exclude&#34;:&#34;,81,85, &#34;}},&#34; product_url&#34;:&#34; https://crmpicco.dev/p/this-is-the-slug/&#34;},{&#34; PRODUCT_ID&#34;:&#34; 51507322001&#34 ;, &#34; free_stock&#34;:&#34; 4&#34;&#34;尺寸&#34;:&#34; 001S&#34;&#34;标题&#34;:&#34; S& #34;&#34; sizes_group_id&#34;:&#34; 30,34&#34;&#34; oos_status&#34;:&#34; 0&#34;&#34;系&#34; :&#34; 51&#34;&#34;蛞蝓&#34;:&#34;这-是最蛞蝓&#34;&#34; sizes_group&#34; {&#34; 30&# 34;:{&#34; ID&#34;:& #34; 30&#34;&#34;标题&#34;:&#34; S&#34;&#34;位置&#34;:&#34; 21&#34;&#34; type_title&# 34;:&#34;服装&#34;&#34; depts_to_include&#34;:&#34;,01,15,11,31,32,21,22,23,24,42,41,51, 61,13,29,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,&#34;&#34;&depts_to_exclude #34;:&#34;,81,85,&#34;}},&#34; product_url&#34;:&#34; https://crmpicco.dev/p/this-is-the-slug/&#34;},{&#34; PRODUCT_ID& #34;:&#34; 51507322002&#34;&#34; free_stock&#34;:&#34; 4&#34;&#34;尺寸&#34;:&#34;002米&#34;,& #34;标题&#34;:&#34; M&#34;&#34; sizes_group_id&#34;:&#34; 31,36&#34;&#34; oos_status&#34;:&#34; 0&#34;&#34;系&#34;:&#34; 51&#34;&#34;蛞蝓&#34;:&#34;这-是最蛞蝓&#34;&#34 ; sizes_group&#34; {&#34; 31&#34; {&#34; ID&#34;:&#34; 31&#34;&#34;标题&#34;:&#34; M&# 34;,&#34;位置&#34;:&#34; 22&#34;&#34; type_title&#34;:&#34;服装&#34;&#34; depts_to_include&#34;:&# 34;,01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19 ,14,16,25,27,18,17,65,98,&#34;&#34; depts_to_exclude&#34;:&#34;,81,85,&#34;}},&#34 ; product_url&#34;:&#34; https://crmpicco.dev/p/this-is-the-slug/&#34;},{&#3 4; PRODUCT_ID&#34;:&#34; 51507322003&#34;&#34; free_stock&#34;:&#34; 9&#34;&#34;尺寸&#34;:&#34;003升&#34 ;,&#34;标题&#34;:&#34; L&#34;&#34; sizes_group_id&#34;:&#34; 32,38&#34;&#34; oos_status&#34;:& #34; 0&#34;&#34;系&#34;:&#34; 51&#34;&#34;蛞蝓&#34;:&#34;这-是最蛞蝓&#34 ;, &#34; sizes_group&#34; {&#34; 32&#34; {&#34; ID&#34;:&#34; 32&#34;&#34;标题&#34;:&#34 ; L&#34;&#34;位置&#34;:&#34; 23&#34;&#34; type_title&#34;:&#34;服装&#34;&#34; depts_to_include&#34; :&#34;,01,15,11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20, 33,19,14,16,25,27,18,17,65,98,#&34;,&#34; depts_to_exclude&#34;:&#34;,81,85,&#34;}}, &#34; product_url&#34;:&#34; https://crmpicco.dev/p/this-is-the-slug/&#34;},{&#34; PRODUCT_ID&#34;:&#34; 51507322004&#34;&#34; free_stock&#34 ;:&#34; 4&#34;&#34;尺寸&#34;:&#34; 004xl&#34;&#34;标题&#34;:&#34; XL&#34;&#34 ; sizes_group_id&#34;:&#34; 35,40&#34;&#34; oos_status&#34;:&#34; 0&#34;&#34;系&#34;:&#34; 51&# 34;,&#34;蛞蝓&#34;:&#34;这-是最蛞蝓&#34;&#34; sizes_group&#34; {&#34; 35&#34; {&#34 ; ID&#34;:&#34; 35&#3 4;,&#34;标题&#34;:&#34; XL&#34;&#34;位置&#34;:&#34; 24&#34;&#34; type_title&#34;:&# 34;服装&#34;&#34; depts_to_include&#34;:&#34;,01,15,11,31,32,21,22,23,24,42,41,51,61,13,29 ,45,46,35,55,28,26,20,33,19,14,16,25,27,18,17,65,98,&#34;&#34; depts_to_exclude&#34;:& #34;,81,85,&#34;}},&#34; product_url&#34;:&#34; https://crmpicco.dev/p/this-is-the-slug/&#34;},{&#34; PRODUCT_ID&#34;:& #34; 51507322005&#34;&#34; free_stock&#34;:&#34; 2&#34;&#34;尺寸&#34;:&#34; 005xxl&#34;&#34;标题&# 34;:&#34; XXL&#34;&#34; sizes_group_id&#34;:&#34; 46&#34;&#34; oos_status&#34;:&#34; 0&#34;&# 34;系&#34;:&#34; 51&#34;&#34;蛞蝓&#34;:&#34;这-是最蛞蝓&#34;&#34; sizes_group&#34;:{ &#34; 46&#34; {&#34; ID&#34;:&#34; 46&#34;&#34;标题&#34;:&#34; XXL&#34;&#34;位置&#34;:&#34; 25&#34;&#34; type_title&#34;:&#34;服装&#34;&#34; depts_to_include&#34;:&#34;,01,15, 11,31,32,21,22,23,24,42,41,51,61,13,29,45,46,35,55,28,26,20,33,19,14,16,25, 27,18,17,65,98,&#34;&#34; depts_to_exclude&#34;:&#34;,81,85,&#34;}},&#34; product_url&#34;:& #34; https://crmpicco.dev/p/this-is-the-slug/&#34;}]

2 个答案:

答案 0 :(得分:2)

基于size数组结构,您必须执行以下操作: ${sizes['somethingHere'][0]}

您可以使用JSON.stringify(sizes)转储并查看[object Object]包含的内容。

编辑:

在代码中将${sizes[0]}${product_id}替换为${sizes[0]['product_id']}。那应该是你的数组中的第一个元素(或者只有一个元素的唯一元素)。

答案 1 :(得分:1)

使用height: 100%:,它会为您提供产品数组中第一个产品的产品ID。

很高兴,你的问题得到了解决。

快乐编码: - )