HTML文件
<select class="form-control" tabindex="-1" id="superId" name="superId[]" multiple="multiple" required="required"
data-bind="options: SupArray,
optionsText: 'emp_display_name',
value: supervisor,
optionsCaption: 'Choose...'"></select>
Javascript文件
function UI() {
var self = this;
self.SupArray = ko.observableArray([]);
self.EmployeeArray = ko.observableArray([]);
self.emp = ko.observable("");
self.supervisor = ko.observable("");
self.startDate = ko.observable();
self.endDate = ko.observable();
self.emp.subscribe(function (newValue) {
// alert(ID);
GetSupervisor(self.emp());
});
}
var DisplayUI = new UI()
ko.applyBindings(DisplayUI);
function GetSupervisor(emp) {
// alert(emp.empId);
var jsonData = ko.toJSON(DisplayUI);
$.ajax({
url: "@Url.Action("GetSupervisorAll", "Registration")",
contentType: "application/json; charset=utf-8",
type: "POST", dataType: "json",
data:JSON.stringify({ empId: emp.empId}),
success: function (data) {
// debugger;
DisplayUI.SupArray(data);
var abc = DisplayUI.SupArray();
// alert(abc);
$('#superId').select2();
}
});
}
这时我正在加载管理员后端数据。
在ajax中,呼叫数据即将成功。
当我调试时,这些值被传递给&#34; success:function(data)&#34;。
但价值并没有附加在下降中。
success: function (data)
console.log(data)
=&GT;
答案 0 :(得分:0)
最后,我找到了这个问题的答案。
function GetSupervisor(emp) {
// alert(emp.empId);
**if (emp != null) {**
var jsonData = ko.toJSON(DisplayUI);
$.ajax({
url: "@Url.Action("GetSupervisorAll", "Registration")",
contentType: "application/json; charset=utf-8",
type: "POST", dataType: "json",
data:JSON.stringify({ empId: emp.empId}),
success: function (data) {
// console.log(data);
// debugger;
DisplayUI.SupArray(data);
var abc = DisplayUI.SupArray();
// alert(abc);
$('#superId').select2();
}
});
}