2016-05-03 72 views
0

因此,我正在使用淘汰賽並嘗試在change事件中獲取javascript中的選定項ID。這裏是我的html如何使用淘汰賽從下拉列表中獲取選定的值

<div id="platforms" data-bind="with: platformsViewModel"> 
      <p> 
       Selected Platform: 
       <select data-bind="options: platforms, optionsText: 'displayName', value: 'id', optionsCaption: 'Choose...', event: { change: loadMedias }" ></select> 
      </p> 
     </div> 

我的看法模型如下

my.viewModels.PlatformsViewModel = function() { 
    var self = this; 

    self.platforms = ko.observableArray(); 
    self.message = ko.observable(); 

    self.loadMedias = function (data, event) { 
     my.loadMedias(data.id); 
    } 
} 

我失去的是什麼?

回答

4

看起來像這可能是一個簡單的修復,在那裏你可能使用value結合,你應該使用optionsValue綁定:

<select data-bind="options: platforms, optionsText: 'displayName', optionsValue: 'id', optionsCaption: 'Choose...', event: { change: loadMedias }" ></select> 
                   <!--^here --> 

但是,爲什麼不把邏輯視圖模型,而不是你的觀點:

my.viewModels.PlatformsViewModel = function() { 
    var self = this; 

    self.platforms = ko.observableArray(); 
    self.message = ko.observable(); 

    //new observable to hold selected platform 
    self.selectedPlatform = ko.observable(); 
    //subscribe to changes in the observable value to trigger the loading 
    self.selectedPlatform.subscribe(function(newValue) { 
     my.loadMedias(newValue.id); 
    }); 
} 

和更新<select>塔噸將結合實際平臺對象中選擇,而不是僅僅其ID:

<select data-bind="options: platforms, optionsText: 'displayName', value: selectedPlatform, optionsCaption: 'Choose...'" ></select> 
+0

是一個偉大的建議,並奇蹟般有效!非常感謝。 –

-1

HTML:

<select name="ddlUsers" id="ddlUsers" class="form-control" 
          data-bind="options: ViewModel.CashierPage.AvailableCash, optionsText: 'OptionTextInfo', value: ViewModel.CashierPage.CashSelected, optionsCaption: 'Cassa...'"></select> 

在JS:

public CashSelected: KnockoutObservable(); 
... 
self.CashSelected = ko.observable(null); 
self.CashSelected.subscribe(function(valueNewValue){/*your code*/}); 
在視圖模型