javascript - TypeError : data. itemNo 不是函数

标签 javascript knockout.js

当我尝试从数据上下文值访问我的可观察值时,出现此错误。 类型错误:data.itemNo 不是函数

这是我的 knockout :

var TallyModel = function (data) {
        var self = this;

        self.itemNo = ko.observable();
        self.qty = ko.observable();
        self.price = ko.observable();
        self.bro = ko.observable();
        self.desc = ko.observable();
        self.extend = ko.observable();
        self.total = ko.observable();
        self.seq = ko.observable();
    }

    var TallyViewModel = function (items) {
        var self = this;

        if(items != null) {
            self.items = ko.observableArray(items.map(function(item) { return new TallyModel(item) }));
        }
        else {
            self.items = ko.observableArray([]);
        }

        self.addLine = function() {
            var tModel = new TallyModel();
            self.items.push( tModel );
        };

        self.insertLine = function(index) {
            self.items.splice(index, 0, new TallyModel() );
        };

        self.removeItem = function(index) {
            console.log(self.items().length);
            if(self.items().length > 1) {
                self.items.splice(index, 1);
            }
        };

        self.checkItemNo = function(data, index) {
            console.log(data);
            **var itemNo = $.trim(data.itemNo());**
            console.log(itemNo);
            $.each(validItems, function (i, elem) {
                if (elem.itemNo == itemNo) {
                    data.price(elem.retail);
                    data.bro(elem.brocCode);
                    data.desc(elem.itemDesc);
                    data.extend(elem.extPrice);
                    data.seq(index);
                }
                else {
                    console.log("could not find " + itemNo + " - " + elem.itemNo);
                }
            });
        };
    }

根据要求,这里是 HTML:

<tbody data-bind="foreach: items">
            <tr>
                <td><input type="text" data-bind="value: itemNo, insertPress: $index, deletePress: $index, event: { blur: $parent.checkItemNo.bind($data, $index) }, attr: { name: 'itemNo[' + $index() + ']', id: 'itemNo[' + $index() + ']' }" class="form-control" /></td>
                <td><input type="text" data-bind="value: qty, insertPress: $index, tabEnterPress: '#tallyEntry', deletePress: $index, attr: { name: 'itemQty[' + $index() + ']', id: 'itemQty[' + $index() + ']' }" class="form-control" /></td>
                <td><input type="text" data-bind="value: price, attr: { name: 'itemPrice[' + $index() + ']', id: 'itemPrice[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td>
                <td><input type="text" data-bind="value: bro, attr: { name: 'itemBro[' + $index() + ']', id: 'itemBro[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td>
                <td><input type="text" data-bind="value: desc, attr: { name: 'itemDesc[' + $index() + ']', id: 'itemDesc[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td>
                <td><input type="text" data-bind="value: extend, attr: { name: 'itemExtend[' + $index() + ']', id: 'itemExtend[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td>
                <td><input type="text" data-bind="value: total, attr: { name: 'itemTotal[' + $index() + ']', id: 'itemTotal[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td>
                <td><input type="text" data-bind="value: seq, attr: { name: 'itemSeq[' + $index() + ']', id: 'itemSeq[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td>
            </tr>
        </tbody>

我已将代码中的罪魁祸首行加粗/加星标。它正在工作,我一定改变了一些东西,不知道为什么它不再工作了。如果我还需要发布相关的 HTML,请告诉我。

最佳答案

您正在使用bind绑定(bind)函数

blur: $parent.checkItemNo.bind($data, $index)

但是bind的第一个参数是thisArg。使用这种语法可能会更容易

blur: function(){ $parent.checkItemNo($data, $index); }

关于javascript - TypeError : data. itemNo 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42512612/

相关文章:

javascript - 循环中元素的显示顺序

javascript - jQuery 在 div 鼠标悬停时更改输入值

jquery - 使用 JQuery UI 拖放和 Knockout 筛选结果

knockout.js - 将 FlattenedTreeTableDataSource 与 CollectionTreeDataSource 一起使用时出现问题

HTML5 离线单页应用程序 (SPA) - 安全问题

javascript - AngularJS $formatters 和 $parsers 使用输入[number]箭头错误地修改模型

javascript - 如何在node.js + Express中获取调用者ip?

javascript 属性不可枚举仍然由 for... 在中找到

c# - KnockoutJS 并将单个对象绑定(bind)到 View 模型

javascript - Q.Promise 和 KO.mapping 未将对象数组转换为正确的类型