1
這是我的代碼,但是當我後鍵入文本輸入框中我點擊保存按鈕,然後文本保存到下拉列表中,但這段文字豈不等於存儲到本地存儲和使用knockout.js如何使用knockout js按鈕點擊localstorage中存儲值?
<form style="margin-top: -25px;">
<button id="buttonSave" type="submit" style="margin-left: 1156px;">Save</button>
</form>
<div id="labelList" class="btn-group" style="margin-top: -595px;margin-left: 3px;">
<input id="editExistannotation" data-bind="value: annotationList" class="editAnnotationList textArea" type="text" placeholder="Edit existing annotation"/>
<select data-bind="options: area"></select>
</div>
----------------------------------------------------
var addHandle = function() {
this.items = ko.observableArray();
this.add = function (item) { this.items.push(item); }
this.remove = function (item) { this.items.remove(item); }
this.clear = function() { this.items.removeAll(); }
}
var addHandler = new addHandle();
ko.applyBindings(addHandler, document.getElementById("slider"));
$("#buttonSave").click(function() {
var label_object;
var labelText = document.getElementById("textarealabel");
var labelObject = new Object();
labelObject.textarealabel = labelText.value;
localStorage.setItem('label_object', JSON.stringify(labelObject));
$("#ddlList").prepend("<option value='0'>" + localStorage.getItem(label_object) + "</option>");
return false;
})
var existAnnotationmodel = new function() {
var labelObject = $('#textarealabel').val();
this.annotationList = ko.observable();
this.area = ko.observableArray();
this.append = ko.computed(function() {
this.area.push(this.annotationList());
localStorage.setItem('labelObject', JSON.stringify(labelObject));
}, this);
}
ko.applyBindings(existAnnotationmodel);
該即時通訊
你不應該使用localStorage.getItem( 'label_object'),而不是localStorage.getItem(label_object)?另一件事是你太削減HTML,很難分析你在做什麼。不過,您有兩個綁定:一個用於特定元素,另一個用於整個文檔。淘汰賽不支持覆蓋對方的多個綁定。也許是這樣的?你可以嘗試準備jsfiddle嗎? – SOReader