0
我是新來淘汰賽,並有問題在同一頁上創建多個「喜歡」按鈕計數器。多個「喜歡」的計數器在同一頁上使用knockout.js
這是我的代碼。
<ul>
<li>
<h4>Item 1</h4>
<div><span data-bind='text: numberOfClicks1'> </span> likes</div>
<button data-bind='click: registerClick'>Like?</button>
</li>
<li>
<h4>Item 2</h4>
<div><span data-bind='text: numberOfClicks2'> </span> likes</div>
<button data-bind='click: registerClick'>Like?</button>
</li>
</ul>
<script type='text/javascript' src='js/knockout-2.2.1.js'></script>
<script type="text/javascript">
var item1 = function() {
this.numberOfClicks1 = ko.observable(0);
this.registerClick = function() {
this.numberOfClicks1(this.numberOfClicks1() + 1);
};
};
ko.applyBindings(new item1());
var item2 = function() {
this.numberOfClicks2 = ko.observable(0);
this.registerClick = function() {
this.numberOfClicks2(this.numberOfClicks2() + 1);
};
};
ko.applyBindings(new item2());
</script>
不知道我需要做什麼?