使用jquery读取html5 localstorage的值的方法 - Web前端
作者:98wpeu发布时间:2026-08-05分类:网页前端技术浏览:225
导读:在html5中,localStorage是个不错的东西,在支持localStorage的浏览器中,能持久化用户表单的输入,即使关掉浏览器,下次重新打开浏览器访问,也能读出其值,...
在html 5中,localStorage是个不错的东西,在支持localStorage的浏览器中, 能持久化用户表单的输入,即使关掉浏览器,下次重新打开浏览器访问,也能读出其值, 下面给出的例子是使用JQuery 在每次表单加载的时候,读localstorage的值,而在表单每次提交时则清楚其值的例子
首先是一个表单:
复制代码 代码如下:
<!DOCtypeHTML>
<html lang="en">
<head>
<metacharset="utf-8">
<title>html5 Local Storage Example</title>
<!-- include BootstrapCSS for layout -->
<link href="//netdna.bootstrapCDN.com/Twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="StyleSheet">
</head>
<body>
<div class="contAIner">
<h1>HTML5 Local Storage Example</h1>
<FORM method="post" class="form-horizontal">
<fieldset>
<legend>EnqUIry Form</legend>
<div class="control-group">
<label class="control-label" for="type">Type of enquiry</label>
<div class="controls">
<select name="type" id="type">
<option value="">Please select</option>
<option value="general">General</option>
<option value="sales">Sales</option>
<option value="support">Support</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input class="input-xlarge" type="text" name="name" id="name" value="" maxlength="50">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email Address</label>
<div class="controls">
<input class="input-xlarge" type="text" name="email" id="email" value="" maxlength="150">
</div>
</div>
<div class="control-group">
<label class="control-label" for="message">Message</label>
<div class="controls">
<Textarea class="input-xlarge" name="message" id="message"></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input name="subscribe" id="subscribe" type="Checkbox">
Subscribe to our newsletter
</label>
</div>
</div>
</fIEldset>
<div class="form-actions">
<input type="submit" name="submit" id="submit" value="Send" class="BTn btn-PRimary">
</div>
</form>
</div>
然后是JS部分代码:
复制代码 代码如下:
<script src="//code.jQuery.com/jquery-latest.js"></script>
<script>
$(document).ready(function () {
/*
* 判断是否支持localstorage
*/
if (localStorage) {
/*
* 读出localstorage中的值
*/
if (localStorage.type) {
$("#type").find("option[value=" + localStorage.type + "]").attr("selected", true);
}
if (localStorage.name) {
$("#name").val(localStorage.name);
}
if (localStorage.email) {
$("#email").val(localStorage.email);
}
if (localStorage.message) {
$("#message").val(localStorage.message);
}
if (localStorage.subscribe === "checked") {
$("#subscribe").attr("checked", "checked");
}
/*
* 当表单中的值改变时,localstorage的值也改变
*/
$("input[type=text],select,textarea").change(function(){
$this = $(this);
localStorage[$this.attr("name")] = $this.val();
});
$("input[type=checkbox]").change(function(){
$this = $(this);
localStorage[$this.attr("name")] = $this.attr("checked");
});
$("form")
/*
* 如果表单提交,则调用clear方法
*/
.submit(function(){
localStorage.clear();
})
.change(function(){
console.log(localStorage);
});
}
});
首先是一个表单:
复制代码 代码如下:
<!DOCtypeHTML>
<html lang="en">
<head>
<metacharset="utf-8">
<title>html5 Local Storage Example</title>
<!-- include BootstrapCSS for layout -->
<link href="//netdna.bootstrapCDN.com/Twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="StyleSheet">
</head>
<body>
<div class="contAIner">
<h1>HTML5 Local Storage Example</h1>
<FORM method="post" class="form-horizontal">
<fieldset>
<legend>EnqUIry Form</legend>
<div class="control-group">
<label class="control-label" for="type">Type of enquiry</label>
<div class="controls">
<select name="type" id="type">
<option value="">Please select</option>
<option value="general">General</option>
<option value="sales">Sales</option>
<option value="support">Support</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input class="input-xlarge" type="text" name="name" id="name" value="" maxlength="50">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email Address</label>
<div class="controls">
<input class="input-xlarge" type="text" name="email" id="email" value="" maxlength="150">
</div>
</div>
<div class="control-group">
<label class="control-label" for="message">Message</label>
<div class="controls">
<Textarea class="input-xlarge" name="message" id="message"></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input name="subscribe" id="subscribe" type="Checkbox">
Subscribe to our newsletter
</label>
</div>
</div>
</fIEldset>
<div class="form-actions">
<input type="submit" name="submit" id="submit" value="Send" class="BTn btn-PRimary">
</div>
</form>
</div>
然后是JS部分代码:
复制代码 代码如下:
<script src="//code.jQuery.com/jquery-latest.js"></script>
<script>
$(document).ready(function () {
/*
* 判断是否支持localstorage
*/
if (localStorage) {
/*
* 读出localstorage中的值
*/
if (localStorage.type) {
$("#type").find("option[value=" + localStorage.type + "]").attr("selected", true);
}
if (localStorage.name) {
$("#name").val(localStorage.name);
}
if (localStorage.email) {
$("#email").val(localStorage.email);
}
if (localStorage.message) {
$("#message").val(localStorage.message);
}
if (localStorage.subscribe === "checked") {
$("#subscribe").attr("checked", "checked");
}
/*
* 当表单中的值改变时,localstorage的值也改变
*/
$("input[type=text],select,textarea").change(function(){
$this = $(this);
localStorage[$this.attr("name")] = $this.val();
});
$("input[type=checkbox]").change(function(){
$this = $(this);
localStorage[$this.attr("name")] = $this.attr("checked");
});
$("form")
/*
* 如果表单提交,则调用clear方法
*/
.submit(function(){
localStorage.clear();
})
.change(function(){
console.log(localStorage);
});
}
});
相关推荐
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 9jQuery编写widget的一些技巧分享 - Web前端
- 最近发表
-
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端


