我正在尝试使用带有本地存储支持的jQuery-Mobile构建一个简单的待办事项列表应用程序,但我尝试了很多次我将数据存储在本地存储中并将其显示在列表中时失败这里是代码我已经提供任何帮助来纠正我的代码将不胜感激
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>ToDoL</title>
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.3.css">
<link rel="stylesheet" type="text/css" href="css/index.css">
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.4.3.min.js"></script>
<script type="text/javascript">
var tasks[];
$(document).on("pagecreate","#todoadderpage",function(){
var appendTaskToList=function(val){
$("#todolist").append("<li>"+val+"</li>");
}
var addTask=function(){
//alert("Hello");
var val=$("#todotitle").val();
tasks.push(val);
localStorage['tasks']=JSON.stringify(tasks);
appendTaskToList(val);
}
$("#todoadderokbtn").click(addTask);
});
</script>
</head>
<body>
<section id="landing" data-role="page">
<header data-role="header" data-position="fixed" data-theme="b">
<h1>ToDoL Logo</h1>
</header>
<article data-role="main" class="ui-content">
<center>
<a href="#todoadderpage" id="adderbtn" data-role="button" data-mini="true" data-inline="true" data-rel="dialog" data-transition="pop">Add To-Do</a>
<input type="button" id="remover" value="Remove Completed Task" data-mini="true" data-inline="true"/>
</center>
<hr/>
<ul id="todolist" data-role="listview" data-inset="true">
<li data-role="list-divider">Your To-Do-List</li>
<li id="notodolist"><p>You Don't have any pending task</p></li>
</ul>
<br/><br/><br/>
</article>
<footer data-role="footer" data-position="fixed" data-theme="b">
<h1>© Reserved By Dinesh Raja</h1>
</footer>
</section>
<section id="todoadderpage" data-role="page" data-close-btn="none">
<header data-role="header" data-position="fixed" data-theme="b">
<h1>New To-Do</h1>
</header>
<article data-role="main" class="ui-content">
<form id="todoadderform">
<div data-role="fieldcontain">
<label for="todotitle">Title</label>
<input type="text" id="todotitle" data-mini="true"/><br/><br/><br/>
<center>
<a href="#" id="todoaddercancelbtn" data-role="button" data-mini="true" data-inline="true" data-rel="back">Cancel</a>
<input type="button" id="todoadderokbtn" value="Ok" data-mini="true" data-inline="true"/>
</center>
</div>
</form>
</article>
</section>
<section id="todoviewerpage" data-role="page" data-close-btn="none">
<header data-role="header">
<h1>To-Do-Viewer</h1>
</header>
<article data-role="main" class="ui-content">
<center>
<label for="tododata"></label>
</center>
<p id="tododata"></p>
<center>
<a href="#" id="todoviewerokbtn" data-role="button" data-rel="back">Ok</a>
</center>
</article>
</section>
</body>
</html>
答案 0 :(得分:1)
这应该有效
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>ToDoL</title>
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.3.css">
<link rel="stylesheet" type="text/css" href="css/index.css">
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.4.3.min.js"></script>
<script type="text/javascript">
var tasks[];
$(document).on("pagecreate","#todoadderpage",function(){
var appendTaskToList=function(val){
$("#todolist").append("<li>"+val+"</li>");
}
var addTask=function(){
//alert("Hello");
var val=$("#todotitle").val();
tasks.push(val);
local-Storage['tasks']=JSON.stringify(tasks);
appendTaskToList(val);
}
$("#todoadderokbtn").click(addTask);
});
</script>
</head>
<body>
<section id="landing" data-role="page">
<header data-role="header" data-position="fixed" data-theme="b">
<h1>ToDoL Logo</h1>
</header>
<article data-role="main" class="ui-content">
<center>
<a href="#todoadderpage" id="adderbtn" data-role="button" data-mini="true" data- inline="true" data-rel="dialog" data-transition="pop">Add To-Do</a>
<input type="button" id="remover" value="Remove Completed Task" data-mini="true" data-inline="true"/>
</center>
<hr/>
<ul id="todolist" data-role="listview" data-inset="true">
<li data-role="list-divider">Your To-Do-List</li>
<li id="notodolist"><p>You Don't have any pending task</p></li>
</ul>
<br/><br/><br/>
</article>
<footer data-role="footer" data-position="fixed" data-theme="b">
<h1>© Reserved By Dinesh Raja</h1>
</footer>
</section>
<section id="todoadderpage" data-role="page" data-close-btn="none">
<header data-role="header" data-position="fixed" data-theme="b">
<h1>New To-Do</h1>
</header>
<article data-role="main" class="ui-content">
<form id="todoadderform">
<div data-role="fieldcontain">
<label for="todotitle">Title</label>
<input type="text" id="todotitle" data-mini="true"/><br/><br/><br/>
<center>
<a href="#" id="todoaddercancelbtn" data-role="button" data-mini="true" data-inline="true" data-rel="back">Cancel</a>
<input type="button" id="todoadderokbtn" value="Ok" data-mini="true" data-inline="true"/>
</center>
</div>
</form>
</article>
</section>
<section id="todoviewerpage" data-role="page" data-close-btn="none">
<header data-role="header">
<h1>To-Do-Viewer</h1>
</header>
<article data-role="main" class="ui-content">
<center>
<label for="tododata"></label>
</center>
<p id="tododata"></p>
<center>
<a href="#" id="todoviewerokbtn" data-role="button" data-rel="back">Ok</a>
</center>
</article>
</section>
</body>
</html>