我有一个搜索表格:
<form class="navbar-search pull-right" data-bind="submit: findBook">
<input type="text" class="search-query" placeholder="Search" id="search-input">
<i class="icon-search"></i>
<input type="submit" value="Submit">
</form>
我在这里列出这些书:
<ul id="container" class="list" data-bind="foreach: books">
<li>
<div>
<table>
<tr>
<td class="center">
<a data-bind="attr:{href: url}">
<img width="150" class="img-polaroid shadow" data-bind="attr:{src: image}">
</a>
</td>
</tr>
<tr>
<td class="center">
<h6 data-bind="text: name"></h6>
</td>
</tr>
</table>
</div>
</li>
</ul>
findBook
我的 ViewModel 中有一个方法:
self.books = ko.computed(function() {
return ko.utils.arrayFilter(library, function(book) {
return self.selectedCategory() == null ||
book.category === self.selectedCategory();
})
});
self.findBook = function() {
self.books = ko.computed(function() {
return ko.utils.arrayFilter(library, function(book) {
return book.name.match(new RegExp($('#search-input').val(), 'i'));
});
});
};
}
我错过了什么?