[英]How to bind values to ul on dropdown selectedindexchange using knockout.js?
I'm struggling with Knockout JS. 我在Knockout JS上苦苦挣扎。 In my web page, after page loaded event ul showing some list of values from database successfully.
在我的网页中,页面加载事件ul成功显示了数据库中的一些值列表。 Below is the code
下面是代码
<ul data-role="listview" data-divider-theme="b" data-inset="true" data-bind="foreach: Cs">
<li data-theme="c">
<a href="#page1" data-transition="slide" data-bind="attr: { title: CID }">
<span data-bind="text: FName + ' ' + LName +')'"></span>
</a>
</li>
</ul>
function SSSmodel() {
var self = this;
self.Cs = $.parseJSON(localStorage["CsTab"]);
};
Onchange event of dropdown binding new list to that ul. 下拉列表的onchange事件将新列表绑定到该ul。 But its not binding new values.
但是它没有约束力的新价值。 Ul showing old values.
Ul显示旧值。 below is the onchage function of dropdown.
以下是下拉菜单的功能。
function onchange()
{
//b contain array of elements
localStorage["aa"] = b;//i checked b.it contains array of values
ko.applyBindings(new SSSmodel());
}
function SSSmodel() {
var self = this;
self.Contacts = $.parseJSON(localStorage["aa"]);
};
}
it doesn't show any error and also not binding new values to ul. 它不会显示任何错误,也不会将新值绑定到ul。 Why?
为什么? Please tell me where i made mistake and give tips to solve this problem.
请告诉我我在哪里犯了错误,并提供解决此问题的提示。
Updated : It seems you have used a reserved keyword onchange
for function name. 已更新:似乎您已使用保留关键字
onchange
作为函数名称。 Change it with any other name like onValueChange
and it will work. 用其他任何名称(例如
onValueChange
更改它,它将起作用。
call the function from somewhere in your code.
从代码中的某个地方调用该函数。 here is Working Sample
这是工作样本
onchange1();
function onchange1() {
//b contain array of elements
// localStorage["aa"] = b; //i checked b.it contains array of values
ko.applyBindings(new StrikeAppSearchViewModel());
}
function StrikeAppSearchViewModel() {
var self = this;
self.Contacts = [
{ ContactID: "MyId1", FirstName: "Jhon", LastName: "Singh dfhd" },
{ ContactID: "MyId2", FirstName: "Dick", LastName: "YBJ" },
{ ContactID: "MyId3", FirstName: "Harry", LastName: "Pdafhd"}];
};
For this scenario, You have to made Jquery Ajax call , then only it will retrieve data are bind to UL. 对于这种情况,您必须进行Jquery Ajax调用,然后只有它将检索数据绑定到UL。
function StrikeAppViewModel() {
var self = this;
self.Contacts = ko.observableArray();
self.Contacts = ko.observable($.parseJSON(localStorage["ContactsTable"]));
self.SearchContacts = function () {
$.ajax({
url: 'http://localhost:12345/api/Contact',
type: 'GET',
dataType: 'jsonp',
data: { ID: id },
context: this,
success: function (result) {
debugger;
self.Contacts(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
};
像这样尝试:
self.Contacts = ko.observable($.parseJSON(localStorage["ContactsTable"]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.