未捕获的类型错误:未定义不是函数淘汰 js

Uncaught TypeError: undefined is not a function knockout js

本文关键字:函数 淘汰 js 未定义 类型 错误      更新时间:2023-09-26

我正在尝试将某些数据添加到数组中,如下所示:

这是我的代码:-

创建.html

字段名称: 显示名称: 添加字段
    </fieldset>
    <table class="table" style="margin:20px 0px">
        <thead>
            <tr>
                <th>Field Name</th>
                <th>Field Display Name</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: fields">
            <tr>
                <td data-bind="text: fieldName"></td>
                <td data-bind="text: fieldDisplayName"></td>
                <td>
                    <div style="height:20px; width:20px; border:1px solid #000000; padding: 5px" data-bind="style: { backgroundColor : color }"></div>
                </td>
                <td><button class="btn btn-danger btn-circle" data-bind="click: remove"><i class="glyphicon glyphicon-remove"></i></button></td>
            </tr>
        </tbody>
    </table>

表.js

  var ViewModel = function () {
    var self = this;
    self.fields = ko.observableArray();
    self.fieldName = ko.observable();
    self.fieldDisplayName = ko.observable();
    self.isRangeError = ko.observable(false);
    var Field = function (fieldName, fieldDisplayName) {
        this.fieldName = fieldName;
        this.fieldDisplayName = fieldDisplayName;
        this.remove = function () {
            self.fields.remove(this);
        }
    }
    self.addFields = function () {
        var tr = self.fields();
        for (var i = 0; i < tr.length; i++) {
            if (self.fieldName == tr[i].fieldName && self.fieldDisplayName == tr[i].fieldDisplayName) {
                self.isFieldError(true);
                return;
            }
        }
        self.isFieldError(false);
        self.fields.push(new Field(self.fieldName(), self.fieldDisplayName()));
    }

我收到这样的错误:-

Uncaught TypeError: undefined is not a functionTableChart.js:41 self.addFieldsknockout-3.2.0.debug.js:3713 (anonymous function)jquery-1.10.2.js:5109 jQuery.event.dispatchjquery-1.10.2.js:4780 elemData.handle

我该如何解决这个问题?我对淘汰赛和javascript很陌生。任何帮助,不胜感激。

您声明的可观察量有问题。

self.isRangeError = ko.observable(false);

应该是:

 self.isFieldError = ko.observable(false);

此外,颜色不是在模型上定义的,而是在表中使用的。