从多个下拉列表中选择一个更改的值

从多个下拉列表中选择一个更改的值

本文介绍了从多个下拉列表中选择一个更改的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于查询,我可以选择一个预选值-使用下拉菜单在下拉菜单中预先选择一个值

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

一旦下拉值被更改,我需要使用新选择的值更新'PrimarySiteUsers可观察的集合.

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

请让我知道任何帮助.

推荐答案

您正在寻找的是订阅.它们将监视指定的可观察对象,并在更改可观察对象时运行.它们将新值传递给您指定的任何功能.这是一个简短但无用的示例.

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>

这篇关于从多个下拉列表中选择一个更改的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:11