html5中如何从本地存储中添加和移除数据
展开全部
试一试 是不是这个效果 望采纳
html--
<form>
<input type="text" id="user" value=""><br>
<input type="text" id="pwd" value=""><br>
记住密码<br><input name="yes" id="yes" type="checkbox" value="yes">
<button id="login">登陆</button><br>
<button id="clear">清楚</button><br>
</form>
jquery--
$(function(){
var login =document.getElementById("login");
var pwd = document.getElementById("pwd");
var user =document.getElementById("user");
var clear = document.getElementById("clear");
var yes = document.getElementById("yes");
if(localStorage.value){
user.value=localStorage.value;
}
if(localStorage.pwd){
pwd.value=localStorage.pwd;
}
$(clear).click(function(){
localStorage.removeItem("value");
});
$("#login").click(function(){
$("#yes").each(function(){
if(this.checked){
localStorage.setItem('value',user.value);
localStorage.setItem('pwd',pwd.value);
}else{
localStorage.setItem('value',user.value);
localStorage.removeItem("pwd");
}
});
});
});
style--
body,div,input,button{margin:0;padding:0;}
form{text-align:center;}
input,button{width:50%;}
html--
<form>
<input type="text" id="user" value=""><br>
<input type="text" id="pwd" value=""><br>
记住密码<br><input name="yes" id="yes" type="checkbox" value="yes">
<button id="login">登陆</button><br>
<button id="clear">清楚</button><br>
</form>
jquery--
$(function(){
var login =document.getElementById("login");
var pwd = document.getElementById("pwd");
var user =document.getElementById("user");
var clear = document.getElementById("clear");
var yes = document.getElementById("yes");
if(localStorage.value){
user.value=localStorage.value;
}
if(localStorage.pwd){
pwd.value=localStorage.pwd;
}
$(clear).click(function(){
localStorage.removeItem("value");
});
$("#login").click(function(){
$("#yes").each(function(){
if(this.checked){
localStorage.setItem('value',user.value);
localStorage.setItem('pwd',pwd.value);
}else{
localStorage.setItem('value',user.value);
localStorage.removeItem("pwd");
}
});
});
});
style--
body,div,input,button{margin:0;padding:0;}
form{text-align:center;}
input,button{width:50%;}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询