我对淘汰赛可见绑定有点困惑。我创建了一个示例来演示该问题。主要目标是在用户选择“其他”选项时显示一些 div (otherDetails)。它不工作。更改“mySelection”字段时,不会重新评估可见的。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Test</title>
<script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="Scripts/knockout-2.2.0.js" type="text/javascript"></script>
<script type="text/javascript">
function data()
{
this.mySelection = ko.observable('other');
this.isOtherSelected = ko.computed(function ()
{
return this.mySelection.peek() == 'other';
}, this);
}
var myData = new data();
$(document).ready(function ()
{
$('#selections').change(function ()
{
myData.mySelection = $(this).val();
});
dataBind();
});
function dataBind()
{
ko.applyBindings(myData);
}
</script>
</head>
<body>
<div>
<select id="selections" data-bind="value: mySelection">
<option value='one'>One</option>
<option value='two'>Two</option>
<option value='three'>Three</option>
<option value='other'>Other</option>
</select>
</div>
<div id="otherDetails" data-bind="visible: isOtherSelected">
<span>Some controls and stuff...</span>
</div>
</body>
在此先感谢,亚龙。