使用JQuery绑定mvvm数据源

时间:2017-04-12 09:42:25

标签: jquery data-binding kendo-ui datasource

我有以下内容:

<div class="container" data-bind="source: content" data-template="content-template"></div>

在JS中,我想动态添加更多DIV(更好:克隆.container DIV)并将数据绑定源更改为content2,content3 ......

基本上我想要这个:$(".question.clone2").data("source", content2) ...

这是什么语法?

2 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
  var clone = $('.container').first().clone();//clone first container div
  (clone).insertAfter('.container'); // inser clone after last container div
  var length = $('.container').length; // get how many container divs are there
  $('.container').last().attr('data-bind',"source: content"+length+""); //change last container div data-bind value (in autoincremeneted fashion like content2,content3,....)
});
.container{
  float:left;
  width:40%;
  height:200px;
  background:grey;
  padding:10px;
  margin:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" data-bind="source: content" data-template="content-template"></div>

答案 1 :(得分:0)

试试这个:

var clone = $('.container').clone(); // use .first() if there are more then one .container class

$(clone).data("source", "content2");