点击绑定不起作用

发布于 2024-12-06 14:20:26 字数 1518 浏览 2 评论 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>

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2024-12-13 14:20:26

您想要将脚本标记移动到文档底部或将其放入 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文