繁体   English   中英

两次选择计算的淘汰赛

[英]Two Select computed Knockout

我尝试用Knockout绑定两个计算的select:-在页面加载时,第一个select(DropDownLinee)被填充-当用户在第一个select上选择一个项目时,第二个select(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'时出现此错误:未捕获的错误:除非您指定'write'选项,否则无法将值写入ko.computed。 如果要读取当前值,请不要传递任何参数。

谁能帮我解决这个问题???

在此先感谢您的问候Donato

您要使用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);

    });
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM