我需要帮助
我有 2 个选择框,我希望能够更改一个选择值,另一个将被计算和选择
问题是当我从一个选择框中选择一个值并且第二个选择框中不存在计算时,例如,如果我将风险金额设置为 700,则投资金额应为 1400,它不会显示在投资金额选择框中
我想要发生的是将其显示在选择框中,但不将其添加为选项
顺便说一句,选择应该双向工作
请帮忙
HTML
Invest Amount:
<select style="width:70px;height:22px;" data-bind="options: AvailableInvestAmount,
optionsText: function(item) {
return '$' + item;
},
value: InvestAmount"></select>
<span data-bind="text: InvestAmount"></span>
<br />
<br />
Risk Amount:
<select style="width:70px;height:22px;" data-bind="options: AvailableRiskAmount,
optionsText: function(item) {
return '$' + item;
},
value: RiskAmount"></select>
<span data-bind="text: RiskAmount"></span>
视图模型
var viewModel = {
RiskAmount: ko.observable(200)
};
viewModel.AvailableInvestAmount = ko.observableArray(['200', '400', '500', '600', '700', '800', '900', '1000', '1200', '1500', '2000', '3000', '5000']);
viewModel.AvailableRiskAmount = ko.observableArray(['200', '400', '500', '600', '700', '800', '900', '1000', '1200', '1500', '2000', '3000', '5000']);
viewModel.InvestAmount = ko.computed({
read: function () {
return this.RiskAmount() * 2;
},
write: function (value) {
this.RiskAmount(value / 2);
},
owner: viewModel
});
ko.applyBindings(viewModel);