我正在尝试从
tokeninput
字段,将它们保存到本地存储,然后稍后检索它们。我出错了。代码:
//Save to storage
var selectedValues = $('#Contacts').tokenInput("get");
console.log(JSON.stringify(selectedValues));
localStorage.setItem('ContactsJSON', JSON.stringify(selectedValues));
//Restore
var names = localStorage.getItem("ContactsJSON");
console.log(names);
$.each(names, function (index, value) {
$("#Contacts").tokenInput("add", value);
});
错误消息为:
“in”的操作数无效:应为对象
jquery-1.10.2.js(1009,2)上显示错误
我很确定我的JSON是正确的,根据
tokeninput docs
,使用get api检索时应采用正确的格式:
selector.tokenInput("get");
Gets the array of selected tokens from the tokeninput (each item being an object of the kind {id: x, name: y}).
我在本地存储中看到的数据如下:
[{id":59,"name":"Steve Carrell"},{"id":58,"name":"John Krasinski"},{"id":1,"name":"Jenna Fischer"}]