我在淘汰賽中遇到了一些麻煩,希望這裏的某個人能夠幫助。敲除只能第一次工作
我有兩個jQuery函數。其中一個使用knockout綁定到頁面上的單個元素。另一個綁定到其餘元素,然後調用第一個函數。
該數據來自返回Json的AJAX請求。
我遇到的問題是使用pGroups列表。它在第一次正常工作,但當你再次點擊它失敗並需要刷新才能再次工作。
控制檯錯誤是:NotFoundError:節點未找到
編輯:更新後的代碼,以顯示進度
jQuery的是:
//Load user data into the action window when a user is selected
$('.ListUserLink').click(function() {
var url = '@Url.Action("DisplayUser", "AjaxUser")' + '?UserId=' + $(this).attr("UserId") + '&UserNum=' + $(this).attr("UserNum") + "&SectId=" + $(this).attr("Sect");
$.ajax({
url: url,
contentType: "application/json; charset=utf-8",
type: 'POST',
context: this,
timeout: 60000,
dataType: 'json',
tryCount: 0,
retryLimit: 3,
success: function (data) {
//ko.applyBindings(new UserViewModel(data));
viewModel.user = new userModel(data);
},
error: function (httpRequest, textStatus, errorThrown) {
alert("Error");
}
});
});
//Load sections on department index change
$("#ddbDepartments").change(function() {
var url = '@Url.Action("GetSectionsByDept", "AjaxUser")' + '?deptId=' + $(this).val();
$.ajax({
url: url,
contentType: "application/json; charset=utf-8",
type: 'POST',
context: this,
timeout: 60000,
dataType: 'json',
tryCount: 0,
retryLimit: 3,
success: function (data) {
//ko.applyBindings(new SectionViewModel(data), $(".SectionsDDB")[0]);
viewModel.sections = new userModel(data);
},
error: function (httpRequest, textStatus, errorThrown) {
alert("Error");
}
});
});
//Assign Section details to fields
function sectionsModel(data) {
this.sectionList = ko.observableArray(data.SectionList);
// this.sections = this.sectionList;
// this.selectedItem = parseInt($("#OldSectionId").value);
};
//Assign user details to fields
function userModel(data) {
this.fullName = ko.observable(data.FirstName + " " + data.Surname);
this.firstName = ko.observable(data.FirstName);
this.surname = ko.observable(data.Surname);
this.usernum = ko.observable(data.UserNum);
//Assign JobTitle Dropdown and selected value
this.jobTitlesList = ko.observableArray(data.TitlesList);
this.jobTitles = this.jobTitlesList;
this.selectedItem = data.JobTitleNum;
//Assign Group/Application list
this.pGroups = ko.observableArray(data.GroupList);
this.sections = ko.observableArray([{}]);
this.ext = ko.observable(data.Ext);
this.userId = ko.observable(data.UserId);
this.olduserid = ko.observable(data.UserId);
$("#ddbDepartments").val(data.DeptId);
this.oldsectionid = ko.observable(data.SectionId);
$("#ddbDepartments").change();
this.oldsectionid = ko.observable(data.SectionId);
//$("#SectionsDDB").val(data.SectionId);
};
var wrapper = function() {
this.user = new userModel(userdata);
this.sections = new sectionsModel(sectiondata);
};
var viewModel = new wrapper();
ko.applyBindings(viewModel);
的pGroups HTML這是在第二失敗嘗試是:
<div data-bind="with: user" id="ActionWindow">
<form action="@Url.Action("SaveUserDetails", "AJAXUser")" method="post" class="AjaxSubmit" id="userDetailsForm">
<h2>User: <span data-bind="text: fullName"></span></h2>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="FirstName" id="FirstName" data-bind="value: firstName" /></td>
<td>
<input type="hidden" name="UserNum" id="UserNum" data-bind="value: usernum" />
<input type="hidden" name="OldUserId" id="OldUserId" data-bind="value: olduserid" />
<input type="hidden" name="OldSectionId" id="OldSectionId" data-bind="value: oldsectionid" />
</td>
<td></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="Surname" id="Surname" data-bind="value: surname" /></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Job Title:</td>
<td><select name="JobTitleNum" id="TitlesList" data-bind="options: jobTitles, optionsValue: 'TitleId', optionsText: 'Title', value: selectedItem"></select></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Extension:</td>
<td><input type="text" name="Ext" id="Ext" data-bind="value: ext" /></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Login ID:</td>
<td><input type="text" name="UserId" id="UserId" data-bind="value: userId" /></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Department:</td>
<td>
<select id="ddbDepartments" name="DeptId">
@foreach (var d in Model.DepartmentList)
{
<option value="@d.DeptId">@d.DeptName</option>
}
</select>
</td>
<td>Section: </td>
<td>
<select name="SectionId" class="SectionsDDB" data-bind="options: $root.sections.list, optionsValue: 'SectId', optionsText: 'SectName', value: SectionId"></select>
@*<select name="SectionId" class="SectionsDDB" data-bind="options: sections, optionsValue: 'SectId', optionsText: 'SectName', value: selectedItem"></select>*@
</td>
</tr>
</table>
<input type="submit" value="Update User" />
<br />
</form>
<h2>Current Groups</h2>
<table>
<tbody data-bind="foreach: pGroups">
<tr>
<td data-bind="text:AppName"></td>
<td data-bind="text:GroupName"></td>
</tr>
</tbody>
</table>
其他所有工作。
我發現這一點:http://knockoutjs.com/documentation/plugins-mapping.html
這表明我應該是這樣的映射:
var viewModel = ko.mapping.fromJS(data, mapping);
但嘗試,因爲我可能我無法弄清楚如何將其應用到我的代碼。
任何幫助將不勝感激。
的ko.applyBindings功能不應該叫每次改變時間部門。 每次調用ko.applyBindings時,整個DOM都會檢查綁定。因此,如果您多次執行此操作,您將獲得同一元素的多個綁定。 請檢查此鏈接,以確保您瞭解如何在同一頁面上擁有多個視圖:http://stackoverflow.com/questions/8676988/example-of-knockoutjs-pattern-for-multi-view-applications/8680668#8680668 –
如果你不能解決問題,請添加一個jsfiddle –
所以我需要從AJAX成功移除ko.apply綁定,並將其映射到那裏呢?你的鏈接中的例子似乎是單獨調用Dom的單獨部分,因爲我有一些重疊。 這裏是一個jsFiddle,但我不能爲我的生活得到它的工作http://jsfiddle.net/dariune/8djDw/ –