简体   繁体   English

如何动态创建Jquery Template输入数据

[英]How to create the Jquery Template input data dynamically

In Jquery Template website they gave this example.( http://api.jquery.com/jQuery.template/ ) 在Jquery Template网站上,他们举了这个例子。( http://api.jquery.com/jQuery.template/

<script>
    var movies = [
        { Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
        { Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
        { Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
    ];

    var markup = "<tr><td colspan='2'>${Name}</td><td>Released: ${ReleaseYear}</td><td>Director: ${Director}</td></tr>"

    /* Compile markup string as a named template */
    $.template( "movieTemplate", markup );

    /* Render the named template */
    $( "#showBtn" ).click( function() {
        $( "#movieList" ).empty();
        $.tmpl( "movieTemplate", movies ).appendTo( "#movieList" );
    });
</script>

My only question is how do i create this 我唯一的问题是如何创建这个

var movies = [
    { Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
    { Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
    { Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];

data dynamically using a for loop? 使用for循环动态数据? I tried creating this structure using a loop[very dirty way] and it didnt worked(probably because what i created was a similar looking string) and i guess its expects an array. 我尝试使用循环[非常脏的方式]创建这个结构,它没有工作(可能是因为我创建的是一个类似的字符串),我想它期望一个数组。

This is an JSON Object Array. 这是一个JSON对象数组。

myArray.push({name: depName, value: depValue});

with push you are able to add new Elements. 使用push,您可以添加新元素。 Read your data from somewhere and insert it into your array. 从某处读取数据并将其插入到阵列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM