2017-06-13 57 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'); 

淘汰赛将为你做剩下的事

相关问题