localstorage的值的方法,jQuery实现根据类型自动显示和隐藏表单

在HTML 5中,localstorage是个不错的东西,在支持localstorage的浏览器中,
能持久化用户表单的输入,即使关掉浏览器,下次重新打开浏览器访问,也能读出其值,
下面给出的例子是使用jquery
在每次表单加载的时候,读localstorage的值,而在表单每次提交时则清楚其值的例子
首先是一个表单

jquery实现表单根据单选按钮进行字段的动画切换,昨天写的,感觉比起初学时写的js/jquery有了很大进步。。在最大化扩展性的情况下经可能使代码精简。

复制代码 代码如下:

html

<!DOCTYPE html>
<html lang=”en”>
CA88新登入,<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部分代码:

<div class=”control-group”>
                    <label
class=”control-label”>类型:</label>
                    <div class=”controls control-row-auto”
id=”type”>
                    </div>
                    <span class=”auxiliary-text”></span>
                </div>
                <div class=”control-group ctype ctype1 ctype2″>
                    <label
class=”control-label”>栏目模版:</label>
                    <div class=”controls”>
                        <select name=”column_tpl”
class=”input”>
                        </select>
                    </div>
                    <span class=”auxiliary-text”></span>
                </div>
                <div class=”control-group ctype ctype1″>
                    <label
class=”control-label”>文章模版:</label>
                    <div class=”controls”>
                        <select name=”article_tpl”
class=”input”>
                        </select>
                    </div>
                    <span class=”auxiliary-text”></span>
                </div>
                <input type=”hidden” name=”tpl” id=”tpl” value=””
/>
                <div class=”control-group ctype ctype3″
style=”display: none;”>
                    <label
class=”control-label”><s>*</s>栏目链接:</label>
                    <div class=”controls”>
                        <input name=”url” type=”text”
class=”input-large”
data-rules=”{required:true,minlength:1,maxlength:30}”>
                    </div>
                    <span
class=”auxiliary-text”>内部链接格式:模块/控制器/方法…,外部链接格式:;
                </div>

复制代码 代码如下:

js