动态生成的下拉选项的敲除绑定
Knockout binding of a Dynamically generated Dropdown options
我有 2 个下拉列表。两者都是通过敲除进行数据绑定的。我试图在这里实现两件事:第一个下拉菜单是 Certification,第二个下拉菜单是 Specialization.
默认情况下,第二个下拉菜单应该有 'Please select Specialization' 作为一个选项,但实际上并没有。它发生在第一个下拉列表中。我想我知道为什么。可能是因为 Certification 是一个可观察数组,它通过 options
绑定进行数据绑定。
不幸的是,我不能对第二个下拉菜单执行相同的操作,因为它的值将取决于第一个下拉菜单。尽管我已经为第二个下拉列表提供了 optionsCaption
,但在页面加载时我仍然看不到它。
$(document).ready(function () {
var Provider = {
ProviderID: ko.observable(""),
FirstName: ko.observable(""),
LastName: ko.observable(""),
Certification: ko.observableArray(["M.B.B.S", "M.D.", "R.N.", "M.S.N."]),
SelectedCertification: ko.observable(""),
Specialization: ko.observable(""),
TaxonomyCode: ko.observable(""),
SSN: ko.observable(""),
ContactNumber: ko.observable(""),
ContactEmail: ko.observable(""),
NPI: ko.observable("")
};
ko.applyBindings(Provider);
$("#Certification").change(function () {
if (($("#Certification").val() == "M.D.") || ($("#Certification").val() == "M.B.B.S")) {
$("#Err_Certification").hide();
$("#Certification").removeClass('borderclass');
$("<option>Dermatology</option>").appendTo("#Specialization");
$("<option>Hematology</option>").appendTo("#Specialization");
$("<option>Neurology</option>").appendTo("#Specialization");
} else if (($("#Certification").val() == "R.N.") || ($("#Certification").val() == "M.S.N.")) {
$("#Err_Certification").hide();
$("#Certification").removeClass('borderclass');
//$("#Certification option[value='option1']").remove();
//$("#Certification option[value='option2']").remove();
//$("#Certification option[value='option3']").remove();
$("<option>Pediatric Nursing</option>").appendTo("#Specialization");
$("<option>Critical Care Nursing</option>").appendTo("#Specialization");
$("<option>Occupational Health Nursing </option>").appendTo("#Specialization");
} else {
$("#Specialization").addClass('borderclass');
$("#Err_Specialization").show();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="container">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label labelfont">CERTIFICATION:</label>
<div class="col-sm-6">
<select class="form-control" id="Certification" name="Certification" data-bind="value: SelectedCertification, options: Certification, optionsCaption: 'Select a Certification'">
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">SPECIALIZATION:</label>
<div class="col-sm-6">
<select class="form-control" id="Specialization" name="Specialization" data-bind="value: Specialization, optionsCaption: 'Select a Specialization'">
</select>
</div>
</div>
</form>
</div>
我分叉了 jquery 更改和一些代码并引入了 subscribe
。
查看模型:
var Provider = function () {
var self = this;
self.Certification = ko.observableArray(["M.B.B.S", "M.D.", "R.N.", "M.S.N."]);
self.SpecialzationArray = ko.observableArray();
self.SelectedCertification = ko.observable();
self.Specialization = ko.observable();
self.SelectedCertification.subscribe(function (val) {
self.SpecialzationArray([]);
if (val == "M.D." || val == "M.B.B.S") {
self.SpecialzationArray(["Dermatology", "Hematology", "Neurology"])
} else if (val == "R.N." || val == "M.S.N.") {
self.SpecialzationArray(["Pediatric Nursing", "Critical Care Nursing", "Occupational Health Nursing"])
} else {
self.SpecialzationArray([]);
}
});
};
$(document).ready(function () {
ko.applyBindings(new Provider());
});
查看:
<select data-bind="value: SelectedCertification, options: Certification, optionsCaption: 'Select a Certification'"></select>
<br/>
<select data-bind="value: Specialization,options:SpecialzationArray, optionsCaption: 'Select a Specialization'"></select>
当我订阅 DDL1 的 value
绑定时,当下拉列表发生变化时,我们可以有条件地填充下拉列表 2 所需的数据。
为了工作fiddleClick here
保持视图模型干净总是更好,这有助于处理复杂的场景。
我有 2 个下拉列表。两者都是通过敲除进行数据绑定的。我试图在这里实现两件事:第一个下拉菜单是 Certification,第二个下拉菜单是 Specialization.
默认情况下,第二个下拉菜单应该有 'Please select Specialization' 作为一个选项,但实际上并没有。它发生在第一个下拉列表中。我想我知道为什么。可能是因为 Certification 是一个可观察数组,它通过
options
绑定进行数据绑定。不幸的是,我不能对第二个下拉菜单执行相同的操作,因为它的值将取决于第一个下拉菜单。尽管我已经为第二个下拉列表提供了
optionsCaption
,但在页面加载时我仍然看不到它。
$(document).ready(function () {
var Provider = {
ProviderID: ko.observable(""),
FirstName: ko.observable(""),
LastName: ko.observable(""),
Certification: ko.observableArray(["M.B.B.S", "M.D.", "R.N.", "M.S.N."]),
SelectedCertification: ko.observable(""),
Specialization: ko.observable(""),
TaxonomyCode: ko.observable(""),
SSN: ko.observable(""),
ContactNumber: ko.observable(""),
ContactEmail: ko.observable(""),
NPI: ko.observable("")
};
ko.applyBindings(Provider);
$("#Certification").change(function () {
if (($("#Certification").val() == "M.D.") || ($("#Certification").val() == "M.B.B.S")) {
$("#Err_Certification").hide();
$("#Certification").removeClass('borderclass');
$("<option>Dermatology</option>").appendTo("#Specialization");
$("<option>Hematology</option>").appendTo("#Specialization");
$("<option>Neurology</option>").appendTo("#Specialization");
} else if (($("#Certification").val() == "R.N.") || ($("#Certification").val() == "M.S.N.")) {
$("#Err_Certification").hide();
$("#Certification").removeClass('borderclass');
//$("#Certification option[value='option1']").remove();
//$("#Certification option[value='option2']").remove();
//$("#Certification option[value='option3']").remove();
$("<option>Pediatric Nursing</option>").appendTo("#Specialization");
$("<option>Critical Care Nursing</option>").appendTo("#Specialization");
$("<option>Occupational Health Nursing </option>").appendTo("#Specialization");
} else {
$("#Specialization").addClass('borderclass');
$("#Err_Specialization").show();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="container">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label labelfont">CERTIFICATION:</label>
<div class="col-sm-6">
<select class="form-control" id="Certification" name="Certification" data-bind="value: SelectedCertification, options: Certification, optionsCaption: 'Select a Certification'">
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">SPECIALIZATION:</label>
<div class="col-sm-6">
<select class="form-control" id="Specialization" name="Specialization" data-bind="value: Specialization, optionsCaption: 'Select a Specialization'">
</select>
</div>
</div>
</form>
</div>
我分叉了 jquery 更改和一些代码并引入了 subscribe
。
查看模型:
var Provider = function () {
var self = this;
self.Certification = ko.observableArray(["M.B.B.S", "M.D.", "R.N.", "M.S.N."]);
self.SpecialzationArray = ko.observableArray();
self.SelectedCertification = ko.observable();
self.Specialization = ko.observable();
self.SelectedCertification.subscribe(function (val) {
self.SpecialzationArray([]);
if (val == "M.D." || val == "M.B.B.S") {
self.SpecialzationArray(["Dermatology", "Hematology", "Neurology"])
} else if (val == "R.N." || val == "M.S.N.") {
self.SpecialzationArray(["Pediatric Nursing", "Critical Care Nursing", "Occupational Health Nursing"])
} else {
self.SpecialzationArray([]);
}
});
};
$(document).ready(function () {
ko.applyBindings(new Provider());
});
查看:
<select data-bind="value: SelectedCertification, options: Certification, optionsCaption: 'Select a Certification'"></select>
<br/>
<select data-bind="value: Specialization,options:SpecialzationArray, optionsCaption: 'Select a Specialization'"></select>
当我订阅 DDL1 的 value
绑定时,当下拉列表发生变化时,我们可以有条件地填充下拉列表 2 所需的数据。
为了工作fiddleClick here
保持视图模型干净总是更好,这有助于处理复杂的场景。