尝试将对象添加到存储阵列中,然后通过循环将它们写出
Trying to add objects into storage array and then write them out with the loop
im 尝试将一些"联系人"添加到本地存储中,每次添加新联系人时,我都想将其刷新到 jquery 列表中。如果我不使用本地存储,我会成功地做到这一点。但是现在我总是有一些问题,无法解决它。
例如,现在我收到此错误
未捕获的类型错误:未定义不是函数
谢谢你的任何想法。
网页:
</head>
<body>
<div data-role="page" data-theme="b" id="page1">
<div data-role="header" id ="head">
<h1></h1>
</div> <!--HEADER-->
<div class="ui-content ui-body-c" role="main" align="left">
<a href="#popupBasic" data-rel="popup" class="ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-plus" data-transition="pop">Add contact</a>
<div data-role="popup" id="popupBasic" data-theme="a">
<p>
<label for="uj">Name: </label>
<input type="text" id="name"/>
<label for="text">Phone: </label>
<input type="text" id="tell"/>
<a data-role="button" onClick="addContact()">add</a>
</p>
</div>
<br>
<div id="liss">
<ul id="listC" data-role="listview" data-autodividers="true" data-filter="true" data-inset="true">
</ul>
</div>
</div> <!--CONTENT-->
<div data-role="footer" align="center">
<a data-role="button" onClick="clear()">Delete memmory</a>
<a data-role="button" onClick="showThemAll()">load</a>
</div> <!--FOOTER-->
</div>
</body>
</html>
.JS:
function showThemAll()
{
if (!localStorage.contacts) { localStorage.contacts = contact; }
else
{
for (i = 0; i < localStorage.contacts.length; i++)
{
$("#listC").append(localStorage.contacts[i]);
$("#listC").listview('refresh');
}
}
}
function addContact()
{
if (!localStorage.contacts) { localStorage.contacts = []; }
var nameElement = document.getElementById("name");
var theName = nameElement.value;
var tellElement = document.getElementById("tell");
var theTell = tellElement.value;
var name = theName;
var tell = theTell;
var AreYouHuman = {nam:name , tel:tell};
var liss = "<li><a href='#page2'>"+AreYouHuman.nam+"</a></li>";
localStorage.contacts.push(AreYouHuman);
$("#listC").append(liss);
$("#listC").listview('refresh');
}
function clear()
{
localStorage.clear();
}
function showThemAll()
{
if(localStorage.contacts)
{
var contacts=JSON.parse(localStorage.contacts);
for (i = 0; i < contacts.length; i++)
{
$("#listC").append(contacts[i]);
$("#listC").listview('refresh');
}
}
}
function addContact()
{
var nameElement = document.getElementById("name");
var theName = nameElement.value;
var tellElement = document.getElementById("tell");
var theTell = tellElement.value;
var name = theName;
var tell = theTell;
var contacts=(localStorage.contacts)? JSON.parse(localStorage.contacts) : [];
var AreYouHuman = {nam:name , tel:tell};
var liss = "<li><a href='#page2'>"+AreYouHuman.nam+"</a></li>";
contacts.push(AreYouHuman);
localStorage.contacts=JSON.stringify(contacts);
$("#listC").append(liss);
$("#listC").listview('refresh');
}
相关文章:
- Handlebars访问第一个项目,然后访问后面的每个项目(在每个循环中)
- 获取表中每行的一列的内容,然后循环访问每个内容
- 在Django中,可以在视图中创建一个包含js的变量,然后在循环模板标记中使用它
- 使用函数for循环遍历对象以更改值,然后返回结果
- 尝试将对象添加到存储阵列中,然后通过循环将它们写出
- 运行循环,然后重置并再次运行它
- 如何让 setTimeout 函数运行,然后停止使用循环
- 增加值直到限制然后减少循环
- 在每个循环上完成异步调用,然后转到下一个节点 js
- 从数组中快速打印对象,然后循环
- Vanilla Js:在一个事件中开始一段时间的循环,然后用第二个事件打破它?('mouseover'启
- 使用$.post()将PHP数组发送到jQuery,然后循环使用这些值
- 单击时将值推送到数组,然后循环添加数组值.Javascript
- 等到元素存在,循环,然后存储在变量中
- 使用生成器等待输入,然后再继续 forEach 循环的主体
- 重复数字达到极限,然后循环它
- Flickity gallery - 如何循环浏览一次然后停止自动播放
- 引导 2 轮播循环一次,然后停止
- 使用 setTimeout() 循环,使矩形变大,直到它到达画布边缘,然后重置
- iMacros 循环然后停止,然后添加另一个命令