Select a changed value from multiple dropdown

2019-08-03 21:05发布

问题:

Based on the query , i am able to select a preselect a value --- Preselect a value in a dropdown using knockout

Once the drop down value is getting changed i need to update the 'PrimarySiteUsers observable collection with the newly selected value.

Please let me know for any help.

回答1:

What you're looking for is a subscription. These will watch the specified observable and run whenever the observable is changed. They pass in the new value to whatever function you specifiy. Here is a short, albeit useless example.

function viewModel() {
  var self = this;

  self.DisplayPage = ko.observable("");

  self.PageDetails = ko.observable("");

  self.DisplayPage.subscribe(function(newValue) {
    if (newValue == "Home") {
      self.PageDetails("No Place like home");
    }
    if (newValue == "About") {
      self.PageDetails("What about it?");
    }
    if (newValue == "Contact") {
      self.PageDetails("Contact who?");
    }

  })

}

ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<select data-bind="value: DisplayPage">
  <option value="Home">Home</option>
  <option value="About">About</option>
  <option value="Contact">Contact</option>
</select>

<div>

  <input data-bind="value: DisplayPage">

  <div data-bind="text: PageDetails"></div>

</div>



标签: knockout.js