我在 MVC 中有以下视图,我试图在其中呈现可折叠菜单。
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript" src="@Url.Content("~/Scripts/knockout-2.2.0.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/moment.js")"></script>
<script type="text/html" id="problemTemplate">
<li>ID# <span data-bind="text: ProblemID"/>
<ul data-bind="template: { name: 'visitTemplate', foreach: VisitList, as: 'visit' }"></ul>
</li>
</script>
<script type="text/html" id="visitTemplate">
<li> Visit <span data-bind="text: VisitID"></span>
</li>
</script>
<script type="text/javascript">
function MyViewModel() {
var self = this;
self.problems= ko.observableArray();
$.getJSON("/api/clients/1/history", self.problems);
}
$(document).ready(function () {
ko.applyBindings(new MyViewModel());
$('#usernav').find('ul').hide();
$('li').live("click", function (e) {
$(this).children('ul').toggle();
e.stopPropagation();
});
})
</script>
}
<div class="content">
<div id="title">
<h1>Details </h1>
</div>
<div>
<ul id="usernav" data-bind="template: { name: 'problemTemplate', foreach: problems, as: 'problem' }"></ul>
</div>
<div class="demo-section">
</div>
</div>
我得到一个显示所有节点的常规列表。$('#usernav').find('ul').hide();
在呈现剔除模板后,似乎永远不会触发该事件。我该如何解决?