【发布时间】:2016-06-14 03:59:19
【问题描述】:
来自http://learn.knockoutjs.com/#/?tutorial=collections
变化
<td><select data-bind="options: $root.availableMeals, value: meal, optionsText: 'mealName'"></select></td>
到
<td><select data-bind="options: $root.availableMeals, value: name, optionsText: 'mealName'"></select></td>
导致输入框中断。损坏时,更改餐食时附加费不会更新,并且默认餐食名称与价格不匹配(总是第一餐,有时是第二餐)
// Class to represent a row in the seat reservations grid
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
}
// Overall viewmodel for this screen, along with initial state
function ReservationsViewModel() {
var self = this;
// Non-editable catalog data - would come from the server
self.availableMeals = [
{ mealName: "Standard (sandwich)", price: 0 },
{ mealName: "Premium (lobster)", price: 34.95 },
{ mealName: "Ultimate (whole zebra)", price: 290 }
];
// Editable data
self.seats = ko.observableArray([
new SeatReservation("Steve", self.availableMeals[1]),
new SeatReservation("Bert", self.availableMeals[0])
]);
// Operations
self.addSeat = function() {
self.seats.push(new SeatReservation("", self.availableMeals[0]));
}
}
ko.applyBindings(new ReservationsViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<h2>Your seat reservations</h2>
<table>
<thead><tr>
<th>Passenger name</th><th>Meal</th><th>Surcharge</th><th></th>
</tr></thead>
<!-- Todo: Generate table body -->
<tbody data-bind="foreach: seats">
<tr>
<td><input data-bind="value: name" /></td>
<td><select data-bind="options: $root.availableMeals, value: name, optionsText: 'mealName'"></select></td>
<td data-bind="text: meal().price"></td>
</tr>
</tbody>
</table>
<button data-bind="click: addSeat">Reserve another seat</button>
name 是 SeatReservation 中的有效字段,此 for 循环正在遍历座位预订。为什么不直接显示人名,为什么会转到默认餐值?谢谢
【问题讨论】:
标签: javascript html knockout.js