2017-06-13 60 views
0

我在頁面上有一個下拉列表。我在下拉列表中敲出了數據綁定。用AJAX填充後在敲除下拉列表中設置選定的值

默認情況下,下拉列表中沒有任何項目。我有一個工作的AJAX調用,併爲下拉列表檢索正確的項目列表。

在項目列表已被檢索並加載到下拉列表中後,如何設置下拉列表中的選定項目?

<select class="form-control" data-bind="options: listOfPossibleValues, value: selectedValue, optionsCaption: 'Select a Value'"></select> 

$.ajax({ 
      type: 'GET', 
      dataType: 'json', 
      url: url, 
      data: { 
       someParameter: someParameterValue 
      }, 
      success: function (response) { 
       $.each(response, function (index, center) { 
        self.listOfPossibleValues.push(response[index]); 
       }); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       console.log("There has been an error retrieving the values."); 
      } 
     }); 

回答

0
<select class="form-control" data-bind="options: listOfPossibleValues, value: selectedValue, optionsCaption: 'Select a Value'"></select> 

$.ajax({ 
      type: 'GET', 
      dataType: 'json', 
      url: url, 
      data: { 
       someParameter: someParameterValue 
      }, 
      success: function (response) { 
       $.each(response, function (index, center) { 
        self.listOfPossibleValues.push(response[index]); 
       }); 
       //set value here 
       $(".form-control").val("xyz123"); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       console.log("There has been an error retrieving the values."); 
      } 
     }); 
0

您必須聲明selectedValueobservable並設置像下面的值:

self.selectedValue = ko.observable(); 
self.selectedValue("//what ever property value get from business model"); 
0

結合使用data-bind=...value: selectedValue那麼你只需要指定它淘汰賽變量正如你所選擇的值新值(所選一個):

self.selectedValue('mySelectedValue'); 

淘汰賽將爲你做剩下的事

相關問題