我在一个页面上有多个 div,这些 div 标有特定的类。就像是 :
<div class="modal-body auctionRequested" >
<table data-bind="foreach: auctionBatches">
<tr>
<td data-bind="text: $data.Id"></td>
<td data-bind="text: $data.Name"></td>
</tr>
</table>
<div class="modal-footer">
<a href="#" class="btn btn-success">Call to action</a>
<a href="#" class="btn" data-dismiss="modal">Close</a>
</div>
</div>
</div>
视图模型定义如下:
function AuctionBatch(id, name, logoUrl, startDate, endDate, isBlind, isBuyNow) {
this.id = ko.observable(id);
this.name = ko.observable(name);
this.logoUrl = ko.observable(logoUrl);
this.startDate = ko.observable(startDate);
this.endDate = ko.observable(endDate);
}
function AuctionBatchList() {
this.auctionBatches = ko.observableArray([]);
}
它以下列方式应用:
$(document).ready(function() {
var auctions = [];
$.ajax({
url: '@Url.Action("GetAuctionBatches", "CarStates")',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.success) {
auctions= data.list;
modals = $('.auctionRequested');
$.each(modals, function(i, item) {
var vm = ko.dataFor(item);
vm.auctionBatches(auctions);
});
}
else {
$("#error").css("display", "block");
$("#errorText").text(data.message);
}
},
error: function () {
alert("error");
}
});
var modals = $('.auctionRequested');
$.each(modals, function(i, item) {
ko.applyBindings(new AuctionBatchList(), item);
});
});
但是,尽管我可以在 firebug 中看到从服务器检索到多个元素,但 ui 上没有任何绑定,我也看不到任何错误。我错过了什么?