在jquery模板中传递脚本标记内的数组

时间:2015-06-05 13:20:36

标签: javascript jquery jquery-templates

我正在尝试在下面的脚本中传递一个数组,但它不是作为一个数组。下面是散列的结构,传递给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 个答案:

答案 0 :(得分:0)

通过将代码粘贴到Notepad ++中,我在数组中找到了一个额外的括号。试试这个:

  <xsl:element name="a">
    <xsl:attribute name="href">
      <xsl:value-of select="./Filepath"/>
    </xsl:attribute>
    File
  </xsl:element>

运行良好的缩进练习,这个问题会少发生。