我是 Knockout 的新手,正在创建一个原型。我试图使用 div 上的可见绑定仅在单击“菜单选项”时显示。但是,当我创建条件时,它似乎不起作用。
这是我的来源:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="Scripts/knockout.js"></script>
<script type="text/javascript" src="Scripts/Models.js"></script>
<script type="text/javascript">
$(function () {
function BillingInformation() {
this.accountNumber = ko.observable('4111111111111111');
this.cardType = ko.observable('Visa');
this.expirationDate = ko.observable('2016-01-15');
}
function Invoice(billingDate, amount, paid) {
this.billingDate = billingDate;
this.amount = amount;
this.paid = paid;
}
function Pet(name, breed, age) {
this.name = name;
this.breed = breed;
this.age = age;
}
function PrototypeViewModel() {
this.menu = [{ folderName: 'Customer Information', folderId: 0 },
{ folderName: 'Customer Information Edit', folderId: 1 },
{ folderName: 'Billing Information Edit', folderId: 2 },
{ folderName: 'Pets', folderId: 3 },
{ folderName: 'Invoices', folderId: 4 }];
this.customer = { firstName: 'Wesley', lastName: 'Snipes', email: 'wsnipes@gmail.com' };
this.billingInformation = new BillingInformation();
this.pets = ko.observableArray(
[
new Pet('Poopy', 'Great Dane', 3),
new Pet('Pokey', 'Great Dane', 2)
]);
this.invoices = ko.observableArray(
[
new Invoice('2012-11-15', 24.35, true),
new Invoice('2012-12-15', 24.35, true),
new Invoice('2013-01-15', 34.43, false)
]);
this.selectedMenuOptionId = ko.observable();
this.selectMenuOption = function (menuOption) {
this.selectedMenuOptionId = menuOption.folderId;
};
this.isMenuSelected = function(menuSelected) {
return this.selectedMenuOptionId == menuSelected;
};
}
ko.applyBindings(new PrototypeViewModel());
});
</script>
</head>
<body>
<ul data-bind="foreach: menu">
<li data-bind="text: $data.folderName, click: $root.selectMenuOption"></li>
</ul>
<div data-bind="visible: isMenuSelected(0)">
<p>selected customer menu</p>
</div>
</body>
</html>
我试图简单地设置可见条件来进行字符串比较,但这似乎不起作用。正如您现在所看到的,我正在使用 isMenuSelected 函数试图使其工作,但也失败了。我也没有收到任何脚本错误。请问,我在这里错过了什么?