2011-09-24 71 views
0

我遇到了click綁定問題。我試圖從Knockout網站運行一些示例代碼,這是行不通的。點擊次數沒有更新。我沒有在Firefox中收到任何javascript錯誤。有人可以幫忙嗎?點擊綁定不起作用

這是我的代碼:

<head runat="server"> 
    <script type="text/javascript" src="/Scripts/jquery-1.6.4.js"></script> 
    <script type="text/javascript" src="/Scripts/jquery.tmpl.js"></script> 
    <script type="text/javascript" src="/Scripts/knockout-1.2.1.js"></script> 
    <script type="text/javascript"> 
     var clickCounterViewModel = function() { 
      this.numberOfClicks = ko.observable(0); 

      this.registerClick = function() { 
       this.numberOfClicks(this.numberOfClicks() + 1); 
      } 

      this.hasClickedTooManyTimes = ko.dependentObservable(function() { 
       return this.numberOfClicks() >= 3; 
      }, this); 
     }; 

     ko.applyBindings(new clickCounterViewModel()); 
    </script> 
</head> 
<body> 
<div>You've clicked <span data-bind="text: numberOfClicks">&nbsp;</span> times</div> 

<button data-bind="click: registerClick, enable: !hasClickedTooManyTimes()">Click me</button> 

<div data-bind="visible: hasClickedTooManyTimes"> 
    That's too many clicks! Please stop before you wear out your fingers. 
    <button data-bind="click: function() { numberOfClicks(0) }">Reset clicks</button> 
</div> 

</body> 

回答

2

你想你的腳本標籤移動到文件底部或把它放在一個onload /就緒功能。在DOM的其餘部分被加載後,您至少需要執行ko.applyBindings

+0

哇......這讓我難住了一陣子。謝謝!!!這是淘汰賽的示例代碼...我希望他們已經指定! @ RP尼邁耶 – Hoppe