我嘗試使用KnockOut JS(ko.plus準確地)綁定選項創建下拉菜單。事情按預期運行,直到我把我的解決方案與這個jsfiddle混合在一起:http://jsfiddle.net/jnuc6y05/爲了在列表中放置一個默認選項。問題就出在「這裏」(請參閱代碼)從哪裏獲得 錯誤信息可編輯解析爲未定義的混合KnockOutJS和純Javascript使用
"TypeError: this.fieldStreetApallou is not a function"
正如我說我沒有問題,我認爲混合純JavaScript和KO造成的情況。我試圖解開可編輯而沒有運氣,因爲它解決了未定義。即使ko.toJS也沒有做到這一點(再次未定義)。
我對KO沒有任何認真的經驗,並且還有Javascript,任何幫助都將不勝感激。
PS:減少代碼提供
/////// HTML
<input data-bind="value: fieldStreetApallou, enable: fieldStreetApallou.isEditing" />
<a href="#" data-bind="visible: !fieldStreetApallou.isEditing(), click: fieldStreetApallou.beginEdit">Rename</a>
<div data-bind="visible: fieldStreetApallou.isEditing">
<a href="#" data-bind="click: function() { fieldStreetApallou.endEdit(); postStreetFieldToServerForApallou(); }"> Confirm</a>
<a href="#" data-bind="click: fieldStreetApallou.cancelEdit"> Cancel</a>
</div>
/////// Javascript
<script type="text/javascript">
ko.observableArray.fn.find = function(prop, data) {
var valueToMatch = data[prop];
return ko.utils.arrayFirst(this(), function(item) {
return item[prop] === valueToMatch;
});
};
var availableCompanies = [{
offset: 1,
name: "Company1"
}, {
offset: 2,
name: "Company2"
}
// ...more pairs here
];
//Default pairs for the drop-down menus
var selectedCompanyApallou = {
offset: 1,
name: "Company1"
};
var ViewModel = function(availableCompanies, selectedCompanyApallou) {
this.availableCompaniesApallou = ko.observableArray(availableCompanies);
this.selectedCompanyApallou = ko.observable(this.availableCompaniesApallou.find("offset", selectedCompanyApallou));
this.fieldStreetApallou = ko.editable("Initial value");
postStreetFieldToServerForApallou = function() {
$.ajax({
type: "PUT",
url: "http://www.san-soft.com/goandwin/addresses/" + 15,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data: "Address_id=15&Street=" + this.fieldStreetApallou() //<---- HERE!
}).done(function(data) {
alert("Record Updated Successfully " + data.status);
}).fail(function(err) {
alert("Error Occured, Please Reload the Page and Try Again " + err.status);
});
};
};
ko.applyBindings(new ViewModel(availableCompanies, selectedCompanyApallou));
</script>
謝謝,現在工作了! – dpesios