0
我試圖將文本框中的值提交到將以表格格式顯示的數組中。將進行進一步的計算,但現在我遇到了綁定問題。在Chrome中的控制檯,我得到的錯誤是用knockout.js提交文本框的值
Uncaught TypeError: Property 'item' of object#<ItemEntry> is not a function
我想不通的地方,我應該來定義的newitem(這是從文本框中提交的值)。
這裏的HTML
<td><input type="text" data-bind="value: newItem" /></td>
<button data-bind="click: addItem">Add Item</button>
<table>
<thead><tr>
<th>Item Number</th><th>Price</th><th></th>
</tr></thead>
<tbody data-bind="foreach: itemNumbers">
<tr>
<td data-bind="text: item"></td>
<td data-bind="text: price"></td>
<td><a href="#" data-bind="click: $root.removeItem">Remove</a></td>
</tr>
</tbody>
</table>
<h3 data-bind="visible: totalCost() > 0">
Total Cost: $<span data-bind="text: totalCost().toFixed(2)"></span>
</h3>
而這裏的JavaScript的
function ItemEntry(item, price) {
var self = this;
self.item = item;
self.price = price;
}
// Overall viewmodel for this screen, along with initial state
function EntryViewModel(newItem) {
var self = this;
self.newItem = newItem;
// Editable data
self.itemNumbers = ko.observableArray([
new ItemEntry("New Item", "$22.50")
]);
// Computed data
self.totalCost = ko.computed(function() {
var total = 0;
for (var i = 0; i < self.itemNumbers().length; i++)
total += self.itemNumbers()[i].item.price;
return total;
});
// Operations
self.addItem = function() {
self.itemNumbers.push(new ItemEntry(self.newItem, "$20.00"));
}
self.removeItem = function(item) { self.itemNumbers.remove(item) }
}
ko.applyBindings(new EntryViewModel());
謝謝!得到它的工作。 – user1104854 2013-04-05 13:20:37