2014-08-30 81 views
1

我用的knockout js和它不工作的例子。我不知道爲什麼點擊事件不會觸發。淘汰賽JS點擊綁定不工作

HTML:

<div> 
    You've clicked <span data-bind="text: numberOfClicks"></span> times 
    <button data-bind="click: incrementClickCounter">Click me</button> 
</div> 

的Javascript:

<script type="text/javascript"> 
    var viewModel = { 
    numberOfClicks : ko.observable(0), 
    incrementClickCounter : function() { 
    alert("hi im click"); 
    var previousCount = this.numberOfClicks(); 
    this.numberOfClicks(previousCount + 1); 
    } 
    }; 
</script> 
+0

你在控制檯中看到什麼錯誤? – 2014-08-30 09:10:07

+0

當我點擊按鈕沒有任何反應 – kumar 2014-08-30 09:11:28

+0

請檢查控制檯...右鍵點擊瀏覽器...有控制檯的選項 – 2014-08-30 09:14:00

回答

2

試試這個代碼 經過link

HTML:

<div> 
    You've clicked <span data-bind="text: numberOfClicks"></span> times 
    <button data-bind="click: incrementClickCounter">Click me</button> 
</div> 

的Javascript:

<script src="~/Scripts/jquery-2.1.1.js"></script> 
<script src="~/Scripts/knockout-3.2.0.js"></script> 
<script type="text/javascript"> 
    var viewModel = { 
    numberOfClicks : ko.observable(0), 
    incrementClickCounter : function() { 
    alert("hi im click"); 
    var previousCount = this.numberOfClicks(); 
    this.numberOfClicks(previousCount + 1); 
    } 
    }; 

ko.applyBindings(new viewModel()); 
</script> 
+1

謝謝普拉香特.. ko.applyBindings(視圖模型);工作很好 – kumar 2014-08-30 09:51:25

+0

它是我的榮幸。 – 2014-08-30 10:28:31