点击绑定不起作用
我在使用 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"> </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>
I'm having trouble with the click
binding. I'm trying to run some sample code from the Knockout website, which isn't working. The number of clicks isn't updating. I'm not getting any javascript errors in Firefox. Can someone help?
This is the code I have:
<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"> </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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要将脚本标记移动到文档底部或将其放入 onload/ready 函数中。在加载 DOM 的其余部分后,您至少需要执行 ko.applyBindings 。
You want to move your script tag to the bottom of the document or put it in a an onload/ready function. You need at least
ko.applyBindings
to execute after the rest of the DOM has been loaded.