本文介绍了Knockout.js依赖选择框问题绑定子选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类似于基因敲除购物车示例,我有一个包装清单,价格取决于位置.我无法将依赖选择框(locationOptions)与视图绑定:
Similar to the knockoutjs shopping cart example, I have a list of packages, and the price depends on the location. I am unable to bind the dependent select box (the locationOptions) with the view:
<select data-bind="options: packages,
optionsCaption: 'Select...',
optionsText: 'name',
value: selectedPackage">
</select>
<select data-bind="options: locationOptions,
optionsCaption: 'Select...',
optionsText: 'location',
value: selectedLocation">
</select>
<span data-bind="with: selectedPackage">
<p>You have chosen <b data-bind="text: name"></b> (<span data-bind="text: description">)</span>
In location <b data-bind="text: location"></b></p>
<p>Total is <b data-bind="text: total"></b></p>
</span>
视图模型:
function viewModel(packages, addons) {
this.packages = packages;
this.selectedPackage = ko.observable();
this.selectedLocation = ko.observable();
this.total = ko.computed(function(){
var x = 0;
return x;
});
}
这是jsfiddle http://jsfiddle.net/KN4P6/6/
Here is the jsfiddle http://jsfiddle.net/KN4P6/6/
推荐答案
看看此小提琴
<select data-bind="options: packages,
optionsCaption: 'Select...',
optionsText: 'name',
value: selectedPackage">
</select>
<!-- ko with : selectedPackage -->
<select data-bind="options: locationOptions,
optionsCaption: 'Select...',
optionsText: 'location',
value: $parent.selectedLocation">
</select>
<!-- /ko -->
<span data-bind="with: selectedPackage">
<p>You have chosen <b data-bind="text: name"></b> (<span data-bind="text: description">)</span></p>
<!-- ko with : $parent.selectedLocation -->
In location <b data-bind="text: location"></b>
<p>Total is <b data-bind="text: $parents[1].total"></b></p>
<!-- /ko -->
</span>
希望对您有帮助.
这篇关于Knockout.js依赖选择框问题绑定子选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!