Javascript 无法设置值以选择淘汰
我刚刚开始使用Knockout(KO),所以我不了解这个库的所有细节。 好的,这是我的ViewModel:Javascript 无法设置值以选择淘汰,javascript,model-view-controller,knockout.js,Javascript,Model View Controller,Knockout.js,我刚刚开始使用Knockout(KO),所以我不了解这个库的所有细节。 好的,这是我的ViewModel: function GetDrugs(ifOther, allDrugs, doctors, id, followUpItems, foundation) { function PatientInfo1() { var self = this; self.PatientName = ko.observable(""); self.PatientCode = ko.ob
function GetDrugs(ifOther, allDrugs, doctors, id, followUpItems, foundation) {
function PatientInfo1() {
var self = this;
self.PatientName = ko.observable("");
self.PatientCode = ko.observable(0);
self.PatientAdress = ko.observable("");
self.PatientBirthday = ko.observable("");
self.RecivedDate = ko.observable("");
self.AssignedTo = ko.observable("");
self.DateIniated = ko.observable("");
self.IfOther = ko.observable("");
self.IsInEnrollmentQueue = ko.observable("");
self.IsInGrandTrackerQueue = ko.observable("");
self.FollowUpItemsName = ko.observable("");
self.FollowupId = ko.observable("");
self.NotificationId = ko.observable("");
self.GrantId = ko.observable("");
self.FollowUpItemsDate = ko.observable("");
self.FoundationName = ko.observable("");
self.FoundationId = ko.observable("");
self.GrantApprovedDate = ko.observable("");
self.GrantExpirationDate = ko.observable("");
self.GrantAmountApproved = ko.observable("");
self.Notes = ko.observable("");
self.NotificationEnrollment = ko.observable("");
self.NotificationPatient = ko.observable("");
self.NotificationNurse = ko.observable("");
self.DoctorsName = ko.observable("");
self.DrugsList = ko.observable("");
self.ifOther = ko.observable(ifOther);
self.allDrugs = ko.observableArray(allDrugs);
self.AllDoctors = ko.observableArray(doctors);
self.AllFollowUpItems = ko.observable(followUpItems);
self.AllFoundations = ko.observable(foundation);
self.allDrugs.push("If Other");
self.isShown = ko.computed(function () {
return self.PatientCode() == 0;
});
self.ApprovePatient =
function () {
var dataToApprove = {
PatientName: self.PatientName,
PatientCode: self.PatientCode,
PatientAdress: self.PatientAdress,
PatientBirthday: self.PatientBirthday,
RecivedDate: self.RecivedDate,
AssignedTo: self.AssignedTo,
DateIniated: self.DateIniated,
IfOther: self.IfOther,
IsInEnrollmentQueue: self.IsInEnrollmentQueue,
IsInGrandTrackerQueue: self.IsInGrandTrackerQueue,
FollowUpItemsName: self.FollowUpItemsName,
FollowupId: self.FollowupId,
NotificationId: self.NotificationId,
GrantId: self.GrantId,
FollowUpItemsDate: self.FollowUpItemsDate,
FoundationName: self.FoundationName,
FoundationId: self.FoundationId,
GrantApprovedDate: self.GrantApprovedDate,
GrantExpirationDate: self.GrantExpirationDate,
GrantAmountApproved: self.GrantAmountApproved,
NotificationEnrollment: self.NotificationEnrollment,
NotificationPatient: self.NotificationPatient,
NotificationNurse: self.NotificationNurse,
DoctorsName: self.DoctorsName,
DrugsList: self.DrugsList,
Notes: self.Notes
};
var dataToApproveJS = ko.toJS(dataToApprove);
$.ajax({
url: '/api/patient',
type: 'POST',
data:
dataToApproveJS,
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
success: function (data) {
}
});
};
self.IsInEditingMode = ko.computed(function () {
return self.PatientCode() == 0;
});
};
function patientViewModel(modePatient) {
var self = this;
self.PatientInfo = modePatient;
$.ajax({
url: '/api/patient/' + id,
cache: false,
type: 'GET',
contentType: 'application/json; charset=utf-8',
data: {},
success: function (data) {
self.PatientInfo.PatientName(data.PatientName);
self.PatientInfo.PatientCode(data.PatientCode);
self.PatientInfo.PatientAdress(data.PatientAdress);
self.PatientInfo.PatientBirthday(data.PatientBirthday);
self.PatientInfo.RecivedDate(data.RecivedDate);
self.PatientInfo.AssignedTo(data.AssignedTo);
self.PatientInfo.DateIniated(data.DateIniated);
self.PatientInfo.IsInEnrollmentQueue(data.IsInEnrollmentQueue);
self.PatientInfo.IsInGrandTrackerQueue(data.IsInGrandTrackerQueue);
self.PatientInfo.FollowUpItemsName(data.FollowUpItemsName);
self.PatientInfo.FollowupId(data.FollowupId);
self.PatientInfo.NotificationId(data.NotificationId);
self.PatientInfo.GrantId(data.GrantId);
self.PatientInfo.FollowUpItemsDate(data.FollowUpItemsDate);
self.PatientInfo.FoundationName(data.FoundationName);
self.PatientInfo.FoundationId(data.FoundationId);
self.PatientInfo.GrantApprovedDate(data.GrantApprovedDate);
self.PatientInfo.GrantExpirationDate(data.GrantExpirationDate);
self.PatientInfo.GrantAmountApproved(data.GrantAmountApproved);
self.PatientInfo.NotificationEnrollment(data.NotificationEnrollment);
self.PatientInfo.NotificationPatient(data.NotificationPatient);
self.PatientInfo.NotificationNurse(data.NotificationNurse);
self.PatientInfo.DoctorsName(data.DoctorsName);
self.PatientInfo.DrugsList(data.DrugsList);
self.PatientInfo.ifOther(data.ifOther);
self.PatientInfo.Notes(data.Notes);
}
});
}
ko.applyBindings(new patientViewModel(new PatientInfo1()));
}
此函数在my.cshtml页面中调用。
我创建并选择:
<div class="form-group">
<div class="col-xs-2">
<label class="control-label">Assigned To</label>
</div>
<div class="col-xs-4">
<select class="form-control" data-bind="options: AllDoctors, optionsText: 'DoctorsName', value:alert(DoctorsName), optionsCaption:''"></select>
</div>
</div>
分配给
除了“选择”的值未设置外,所有操作都正常。
在页面加载时,我有空选择功能,可以从分配给医生的文件中进行选择,但我希望有以前设置过的值,它存储在DoctrorsName KO变量中。您需要在绑定中绑定所选的医生 在模型中添加选定的可观察对象:
self.selectedDoctor = ko.observable();
然后将值绑定到可观察对象:
<select class="form-control" data-bind="options: AllDoctors, optionsText: 'DoctorsName', value: selectedDoctor, optionsCaption:''"></select>
然后,如果要获取医生姓名,可以使用self.selectedDoctor.DoctorsName
下面是一个基本的JSFIDLE演示:
你能用jsfiddle.net中的问题创建一个例子吗?敲除在jsfiddle中有效吗?是的,您应该在左侧面板的下拉菜单中选择敲除库如何设置selectedDoctor的值?它将自动设置,因为它是由值绑定设置的。在我的回答中添加了一个JSFIDLE,显示它的工作原理。您可以告诉我如何使例如“Nick Johnson”成为第一个吗?您只需将Nick Johnson放在医生:这就是你的意思吗?