【问题标题】:Knockoutjs not updating UI when programmatically updating an observable以编程方式更新可观察对象时,Knockoutjs 不更新 UI
【发布时间】:2020-06-04 03:52:21
【问题描述】:

当我以编程方式更新可观察对象并打开模式时,为什么下拉菜单没有更新?这是所有代码 https://jsfiddle.net/krob636/jes9bvLw/119/

如果我先尝试更改元素的值,则更改可观察对象会更新下拉列表。单击 Launch modal 按钮,然后单击 Observable 按钮。选定的 id 会更改,但下拉菜单不会更改。现在单击 Element 按钮,然后再次单击 Observable 按钮。下拉列表确实发生了变化。

Javascript

this.selectedSimulators = ko.observableArray().extend({notify: 'always'});

this.simulators = ko.observableArray([
  new Simulator(1, 1, 1, "CH-53E", "APT", "APT 2F190-2"),
  new Simulator(2, 1, 1, "CH-53E", "EAET", "EAET 2H164-2"),
  new Simulator(3, 1, 1, "CH-53E", "WST", "WST 2F174-2")
]);

this.openModal = function() {
  $('#exampleModal').modal('show');
  this.selectedSimulators(1);

  // UI does not update without calling this 
 //$("#ddSims").val(1);
}

HTML

  <select class="form-control" id="ddSims" multiple="multiple" data-bind="options: simulators,
      optionsText: 'typeAndSerialNumber',
      optionsValue: 'id',
      selectedOptions: selectedSimulators,
      multiselect: {includeSelectAllOption: true}">
 </select>

 <button type="button" class="btn btn-primary" data-bind="event: {click: openModal}">
    Launch modal
  </button>

【问题讨论】:

    标签: javascript knockout.js knockout-3.0 bootstrap-multiselect


    【解决方案1】:

    您正在混合 observables 和 observableArrays。 selectedSimulators 是一个数组,但是您将其设置为值 1 而不是将值 1 推送到数组中。请改用self.selectedSimulators.push(1);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-16
      • 2013-12-16
      • 1970-01-01
      • 2017-03-30
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      相关资源
      最近更新 更多