在HTML 5中,localstorage是个不错的东西,在支持localstorage的浏览器中, 能持久化用户表单的输入,即使关掉浏览器,下次重新打开浏览器访问,也能读出其值, 下面给出的例子是使用jquery 在每次表单加载的时候,读localstorage的值,而在表单每次提交时则清楚其值的例子
首先是一个表单:
复制代码 代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Local Storage Example</title>
<!-- include Bootstrap CSS 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="/UploadFiles/2021-04-02/jquery-latest.js"><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);
});
}
});
首先是一个表单:
复制代码 代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Local Storage Example</title>
<!-- include Bootstrap CSS 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="/UploadFiles/2021-04-02/jquery-latest.js"><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);
});
}
});
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“使用jquery读取html5 localstorage的值的方法”评论...
更新动态
2024年11月15日
2024年11月15日
- 丁红《女儿情》[DTS-WAV]
- 陈惠婷.2017-Voyager3【亚神音乐】【FLAC分轨】
- 群星.2001-华纳歌声魅影【华纳】【WAV+CUE】
- 张雨生.2016-雨后星空2CD【丰华】【WAV+CUE】
- 李雨寰《Break Free》[FLAC/分轨][533MB]
- 廖也欧《面朝大海》[320K/MP3][18.35MB]
- 廖也欧《面朝大海》[Hi-Res][24bit 48kHz][FLAC/分轨][170.14MB]
- s13T1夺冠五人名单都有谁 s13T1夺冠五人名单一览
- 英雄联盟T1战队队长都有谁 T1战队所有队长介绍
- skt历届战队成员都有哪些 skt历届战队成员名单盘点
- 妙音唱片《大热唱片3》[WAV+CUE]
- 费玉清《跟着地球旋转》滚石时代经典复刻[正版原抓WAV+CUE]
- 罗文甄妮-射雕英雄传(AMCD)(限量版)[WAV+CUE]
- 《巫师4》定档2025再添佐证:参演人员曝光 老将回归
- 辣眼睛 美女COS《黑神话:悟空》比基尼版金池长老