二选择计算淘汰

Two Select computed Knockout

本文关键字:淘汰 计算 选择      更新时间:2023-09-26

我尝试用Knockout绑定两个计算的select:-页面加载时填充第一个选择(DropDownLinee)-当用户在第一个选择上选择一个项目时,第二个选择(DropDownCorse)被填充

这是一个例子:

<select id="DropDownLinee" data-bind="options: ArrayLinee, optionsText: 'NomeLinea', optionsValue: 'NomeLinea', value: selectedLinea " data-toggle="dropdown"></select>
<select id="DropDownCorse" data-bind="options: ArrayCorse,  optionsText: 'CodiceCorsa', optionsValue: 'CodiceCorsa', value: selectedCorsa " data-toggle="dropdown"></select>
 function LineeViewModel() {
      var self = this;
  self.selectedLinea = ko.observable();
  self.selectedCorsa = ko.observable();
  self.ArrayLinee = ko.observableArray([]);
  self.ArrayCorse = ko.observableArray([]);
  $.getJSON('/Home/GetLines', function (data) {
    self.ArrayLinee(data);
  });
  self.ArrayCorse = ko.computed(function () {
    $.getJSON('/Home/GetRides',
    {
      LineaSelezionata: self.selectedLinea(),
      DirezioneSelezionata: $('input[name=radio4]:checked', '.areaselezione').val()
    },
    function (data) {
      debugger;
      self.ArrayCorse(data);
    });
  });
}

  lineeVM = new LineeViewModel();
  ko.applyBindings(lineeVM);

当我检查加载"DropDownCorse"时,我出现了以下错误:未捕获错误:除非指定"写入"选项,否则无法将值写入ko.computed。如果您希望读取当前值,请不要传递任何参数。

有人能帮我解决这个问题吗???

感谢提前问候多纳托

您想要使用subscribe,而不是computed

  self.selectedLinea.subscribe(function (newSelection) {
    $.getJSON('/Home/GetRides',
    {
      LineaSelezionata: newSelection,
      DirezioneSelezionata: $('input[name=radio4]:checked', '.areaselezione').val()
    },
    function (data) {
      debugger;
      self.ArrayCorse(data);
    });
  });