淘汰js点击绑定无法正常工作

时间:2014-08-30 09:07:39

标签: javascript jquery knockout.js click

我使用knockout 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>

1 个答案:

答案 0 :(得分:3)

试试这段代码 完成 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>