在jQuery模板中传递数组到脚本标签中

3

我正在尝试在下面的脚本中传递一个数组,但它并没有被识别为一个数组。以下是传递给jquery-template的哈希结构。options 是我想要传递的数组。

{
   "options": [
      [
        "category 1",
        "category 1",
        [
          [
            "subcategory 1",
            "subcategory 1",
            [
              [
                "item 1",
                "item 1"
              ],
              [
                "item 2",
                "item 2"
              ]
            ]
          ],
          [
            "subcategory 2",
            "subcategory 2",
            [
              [
                "item 1",
                "item 1"
              ],
              [
                "item 2",
                "item 2"
              ]
            ]
          ]
        ]
      ]
    ]
  }

我有这段jquery-template的代码需要渲染,当我将选项传递给下面的data_tree时,它不作为数组而是作为非字符串,导致出错。

<script id="nestedFieldTemplate" type="text/x-jquery-tmpl">
  <script type="text/javascript">
        jQuery(document).ready(function(){
            jQuery('#div_ff').nested_select_tag({
                initValues: { 
                    "subcategory_val":null,
                    "item_val":null,
                    "category_val":null         
                },
                default_option: "<option value='-1'>...</option>",
                data_tree: ${options},
            });
        });
    {{html "</sc"+"ript>"}}
</script>

我尝试将其作为普通的JavaScript变量传递,但没有成功。我想在下面的代码中将jquery-template变量作为数组传递。

以下是它生成脚本的方式。

jQuery(document).ready(function(){           
    jQuery('#div_ff_ffs_02').nested_select_tag({            
        initValues: {             
            "subcategory_val":null,             
            "item_val":null,             
            "category_val":null                     
        },              
        default_option: "<option _tmplitem="14"  value='-1'>...</option>",              
        data_tree: category 1,category 1,subcategory 1,subcategory 1,item 1,item 1,item 2,item 2,subcategory 2,subcategory 2,item 1,item 1,item 2,item 2,subcategory 3,subcategory 3,,category 2,category 2,subcategory 1,subcategory 1,item 1,item 1,item 2,item 2,                        
    });       
});       

1
非常重要的是对齐大括号,或者使用一个能够匹配大括号的编辑器。我无法理解你的数组,但我知道它不是一个数组,而是一个带有数组属性的对象。 - Dagrooms
1个回答

0
通过将您的代码粘贴到Notepad++中,我发现数组中有一个额外的括号。请尝试以下操作:
{
    "options": 
    [
        [
        "category 1",
        "category 1",
            [
                [
                "subcategory 1",
                "subcategory 1",
                    [
                        [
                        "item 1",
                        "item 1"
                        ],
                        [
                        "item 2",
                        "item 2"
                        ]
                    ]
                ],
                [
                "subcategory 2",
                "subcategory 2",
                    [
                        [
                        "item 1",
                        "item 1"
                        ],
                        [
                        "item 2",
                        "item 2"
                        ]
                    ]
                ]
            ]
        ]
    ]
}

遵循良好的缩进规范,这个问题就会少得多。


做了相应的更改,因为数组很大,所以在发布之前进行了编辑,以避免使上下文不被忽视。因此,错误不是由于数组结构引起的。 - Abhishek
好吧,这是一次尝试。你现在是否收到相同的错误? - Dagrooms
我现在遇到了相同的错误,就像我之前说的,在发布此处内容之前修改了数组结构。 - Abhishek
啊,好的。你试过直接设置一个变量 options = [...]; 而不使用上面的对象表示法吗? - Dagrooms
它是动态的,所以不能在那里设置数组,必须将其作为变量传递。 - Abhishek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接