我可能会为此获得很多反对意见,但我真的很需要你的帮助,因为我正在努力。
我的 HTML 页面中有这段代码:
<div class="lines">
<div class="formElement">
<label>Activity</label>
<select name="activity" id="activity" data-bind="options: activityArray, value: activity, event:{mouseover: interactive, mouseout: interactive}, onclick: interactive" style="width: 15em;"></select>
</div>
<div class="formElement">
<label>Sub Activity</label>
<select name="subActivity" id="subActivity" data-bind="options: subActivityArray, value: subActivity" style="width: 15em;"></select>
</div>
</div>
这是我的视图模型:
viewModelMain = function () {
var
mainData = ko.observableArray([]),
showView = ko.observable(),
activity = ko.observable(),
activityArray = ko.observableArray(['licenses', 'services']),
subActivity = ko.observable(),
subActivityArray = ko.observableArray([]),
request = ko.observable(),
firstPayAmmount = ko.observable(),
recuringAmmount = ko.observable(),
comment = ko.observable(),
interactive = function () {
console.log("inside of interactive");
if (this.activity() == 'licenses') {
this.subActivityArray(['test']);
} else this.subActivityArray(['test1']);
};
return {
mainData: mainData,
showView: showView,
activity: activity,
activityArray: activityArray,
subActivity: subActivity,
subActivityArray: subActivityArray,
request: request,
firstPayAmmount: firstPayAmmount,
recuringAmmount: recuringAmmount,
comment: comment,
interactive: interactive
};
},
我的问题是关于 2 个下拉菜单之间的依赖关系。选择第一个选项时,如何更改第二个下拉列表的值?As you can see in the above code, I have tried event:{mouseover: interactive, mouseout: interactive}
but the problem with it is that when the 2nd option is selected the value in the snd drop down is not updated untill the mouse goes again over the select box. 我确信有一种简单的方法可以做到这一点,但作为初学者,我无法发现它