2

我一直在使用淘汰赛,但在编写一些代码示例时,这让我很难过。代码完全按照我的预期工作,点击按钮切换可见,但我仍然收到错误:

JsFiddle:http: //jsfiddle.net/JasonMore/hCdF8/1/

Uncaught Error: Unable to parse bindings.
Message: ReferenceError: answerClick is not defined;
Bindings value: click: answerClick

<ul data-bind="foreach: answers">
    <li>
        <a class="hiddenButton" href="#" data-bind="click: answerClick" />
        <div class="answerNumber" data-bind="visible: showAnswerNumber">
            <h2 data-bind="text: answerNumber" />
        </div>
        <div class="answer" data-bind="visible: showAnswerText">
            <p data-bind="text:text" />
            <p data-bind="text: points" />
        </div>
    </li>
</ul>

<script type="text/javascript">

    var answerViewModel = function () {
        var self = this;

        //clicks
        self.answerClick = function () {
            self.showAnswerNumber(!self.showAnswerNumber());
        };

        //observables
        self.answerNumber = ko.observable();
        self.text = ko.observable();
        self.points = ko.observable();
        self.showAnswerNumber = ko.observable(true);

        //computed
        self.showAnswerText = ko.computed(function () {
            return !self.showAnswerNumber();
        });
    };

    var roundViewModel = function () {
        var self = this;
        self.answers = ko.observableArray();
    };

    var answer1 = new answerViewModel();
    answer1.text = "foo answer";
    answer1.answerNumber = 1;
    answer1.points = 50;

    var vm = new roundViewModel();
    vm.answers.push(answer1);

    ko.applyBindings(vm);

</script>
4

1 回答 1

3

因为您正在关闭您的锚标记: <a />,所以它以一种奇怪的方式呈现,其中一个额外的锚标记位于 之外,而不是在有效函数foreach的上下文中。answerClick

我不确定您要为链接使用什么内容,但您需要有一个开始和结束标记,例如:

<a class="hiddenButton" href="#" data-bind="click: answerClick">link</a>

不确定您是否打算将内容包装在该链接中,但这就是您的问题所在。

示例:http: //jsfiddle.net/rniemeyer/hCdF8/2/

于 2012-04-12T18:50:30.563 回答