使用jquery讀取html5 localstorage的值的方法
更新時(shí)間:2013年01月04日 11:13:54 作者:
在HTML 5中,localstorage是個(gè)不錯(cuò)的東西,在支持localstorage的瀏覽器中, 能持久化用戶表單的輸入,即使關(guān)掉瀏覽器,下次重新打開瀏覽器訪問,也能讀出其值,很不錯(cuò)的一個(gè)東西,接下來(lái)實(shí)例介紹,需要的朋友可以參考下
在HTML 5中,localstorage是個(gè)不錯(cuò)的東西,在支持localstorage的瀏覽器中, 能持久化用戶表單的輸入,即使關(guān)掉瀏覽器,下次重新打開瀏覽器訪問,也能讀出其值, 下面給出的例子是使用jquery 在每次表單加載的時(shí)候,讀localstorage的值,而在表單每次提交時(shí)則清楚其值的例子
首先是一個(gè)表單:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Local Storage Example</title>
<!-- include Bootstrap CSS for layout -->
<link 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="http://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");
}
/*
* 當(dāng)表單中的值改變時(shí),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")
/*
* 如果表單提交,則調(diào)用clear方法
*/
.submit(function(){
localStorage.clear();
})
.change(function(){
console.log(localStorage);
});
}
});
首先是一個(gè)表單:
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Local Storage Example</title>
<!-- include Bootstrap CSS for layout -->
<link 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部分代碼:
復(fù)制代碼 代碼如下:
<script src="http://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");
}
/*
* 當(dāng)表單中的值改變時(shí),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")
/*
* 如果表單提交,則調(diào)用clear方法
*/
.submit(function(){
localStorage.clear();
})
.change(function(){
console.log(localStorage);
});
}
});
相關(guān)文章
jQuery實(shí)現(xiàn)簡(jiǎn)單登錄條件判斷
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單登錄條件判斷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03jQuery簡(jiǎn)單注冊(cè)和禁用全局事件的方法
這篇文章主要介紹了jQuery簡(jiǎn)單注冊(cè)和禁用全局事件的方法,結(jié)合實(shí)例形式分析了jQuery中ajaxStart與ajaxStop方法的使用技巧,需要的朋友可以參考下2016-07-07jQuery插件FusionWidgets實(shí)現(xiàn)的Bulb圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件FusionWidgets實(shí)現(xiàn)的Bulb圖效果,結(jié)合完整實(shí)例形式分析了jQuery使用FusionWidgets插件結(jié)合swf文件載入xml數(shù)據(jù)實(shí)現(xiàn)Bulb圖效果的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03jQuery 出現(xiàn)Cannot read property ‘msie’ of undefined錯(cuò)誤的解決方法
這篇文章主要介紹了jQuery 出現(xiàn)Cannot read property ‘msie’ of undefined錯(cuò)誤的解決方法的相關(guān)資料,需要的朋友可以參考下2016-11-11jQuery創(chuàng)建及操作xml格式數(shù)據(jù)示例
這篇文章主要介紹了jQuery創(chuàng)建及操作xml格式數(shù)據(jù),結(jié)合實(shí)例形式分析了jQuery針對(duì)xml格式數(shù)據(jù)的創(chuàng)建、解析、添加等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05