0
我希望能够通过单击列表中的名称从可观察数组中移除项目。不过,我注意到每次将项目添加到表格或列表时,click事件都会触发。在我的情况下,我希望能够点击列表中的项目将其删除,但将删除绑定到单击事件将始终导致删除前一个项目。有没有办法做到这一点,而不使用点击事件?下面,我将包含一个jsFiddle,它是一个稍微修改过的基础教程的演示版本。当添加到列表中时,Knockout JS click事件触发
https://jsfiddle.net/hardrock302/93wdcm65/
下面的代码:
<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 data-bind="text:name, click:alert('test')"></td>
</tr>
</tbody>
</table>
// 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[0]),
new SeatReservation("Bert", self.availableMeals[0])
]);
}
ko.applyBindings(new ReservationsViewModel());
另外,你可以使用[punches plugin](https:// m best.github.io/knockout.punches/):'' – Cody