0
我試圖創建一個購物清單,用戶可以輸入物品和數量。我在列表中添加了一些項目(在代碼中),但我希望用戶使用「添加」按鈕來添加內容。如何讓用戶輸入數據到observabaleArrays
在用戶點擊添加時,添加了「新的4」。我希望這些數據是用戶輸入的。我不知道如何讓用戶輸入數據到observabaleArrays。
我對Knockout.js相當陌生,我無法找到解決方法。
<table>
<thead>
<tr> Item </tr>
<tr> Quantity </tr>
</thead>
<tbody data-bind="foreach: items">
<tr>
<td data-bind="text:item"></td>
<td data-bind="text:quantity"></td>
</tr>
</tbody>
</table>
<button data-bind="click: addItem"> ADD </button>
<script>
function appViewModel(){
var self = this;
self.items = ko.observableArray([
{
item: 'Carrot',
quantity : '1'
},
{
item: 'Milk',
quantity : '2'
},
{
item: 'Bread',
quantity : '3'
}
]);
self.addItem = function(){
self.items.push({item: 'New', quantity: '4'});
}
}
ko.applyBindings(new appViewModel());
</script>
</body>
</html>