简体   繁体   English

更新可观察到的淘汰赛和下拉菜单

[英]Update Knockout Observable and Dropdown Select

I have an ASPX page with a dropdown select bound with Knockout.JS. 我有一个ASPX页面,其中的下拉选择与Knockout.JS绑定在一起。 On the page load I check the url for a parameter and update the view if their is a parameter which you can see in my API. 在页面加载时,我检查URL的参数并更新视图(如果它们是您可以在我的API中看到的参数)。 I've changed the API to leave out unnecessary code because it returns the value needed. 我更改了API,以省去不必要的代码,因为它返回所需的值。 My problem is that I cannot get my observable SelectedView to update to "Notes". 我的问题是我无法将可观察的SelectedView更新为“ Notes”。 Any advice? 有什么建议吗?

ASPX: ASPX:

       <asp:DropDownList runat="server" data-bind="value: SelectedView" id="viewselect">
           <asp:ListItem>Select A View</asp:ListItem>
           <asp:ListItem>Notes</asp:ListItem>
           <asp:ListItem>Credit Manager</asp:ListItem>
       </asp:DropDownList>

View Model: 查看模型:

function CustomerViewModel() {

    this.self = this;
    self.SelectedCustomer = ko.observable();
    self.SelectedView = ko.observable();
}

API: API:

$(document).ready(function () {

 var custnmbr = "123456";

 if (custnmbr != "") {
     var notes = "Notes";
     self.SelectedView(notes);
 }

});

I guess if you look in console you 'll get error: 我猜如果您在控制台中查看,将会收到错误消息:

Uncaught TypeError: Object [object global] has no method 'SelectedView' 

Because in your $(document).ready you are using the object self which is only defined inside CustomerViewModel() . 因为在$(document).ready您正在使用仅在CustomerViewModel()内部定义的对象self

To solve this, you need to call .SelectedView(notes); 要解决此问题,您需要调用.SelectedView(notes); on the object instance you are passing to ko.applyBindings , 在要传递给ko.applyBindings的对象实例上,

UPDATE 更新

for example: 例如:

function CustomerViewModel() {
    this.self = this;
    self.SelectedCustomer = ko.observable();
    self.SelectedView = ko.observable();
}
var customerObj=new CustomerViewModel();
ko.applyBindings(customerObj);

// later in your code.
customerObj.SelectedView(notes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM