很大的虫子糖果:如何让网页表单提交错误返回重新填写时,让原有填写的内容保存下来

来源:百度文库 编辑:中科新闻网 时间:2024/04/27 22:51:08
在网页表单中填写内容时,有哪里填错了,提交没有成功,而返回重新填写时,原来所填写的内容又要重新填写,这样很麻烦,我看有些网站,他们返回重新填写时都有记住之前所填写的内容,这是如何实现的,谢谢

可以用history.go(-1)或history.back(1)但这样的话,输入的密码和文件域也不能恢复,建议用javascript在客户端写一个验证函数,在提交(submit)的时候触发验证。然后在服务器端进一步验证,以防有人投机取巧。

这是因为他们是用了浏览器的“后退”功能,就相当于你点左上角那个“后退”按钮一样。

网页代码就是go history(-1)好像是这么写的。

打开IE浏览器,点“工具”菜单---“Internet选项”---“内容”标签卡---下面的“自动完成”按钮,把里面的复选框都选中就好了。
有时也是各网站设置的不同造成的。

看不懂代码管我咯?
直接给出答案,

login.html 简单的提交表单
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

<form action="" id="loginForm" method="post">

<!--如果提交的信息由用户信息将用户信息填充到表单中-->

<label>手机:</label>
<input type="text" class="wk_table_input" name="user[mobile]" placeholder="手机号" id="mobile" value="<%if(user){%><%=user.mobile%><%}%>"/><br/>

<label>密码:</label><input type="password" class="wk_table_input" name="user[pwd]" placeholder="密码" id="pwd" value="<%if(user){%><%=user.pwd%><%}%>"/><br/>

<input type="submit" placeholder=" 登录" class="workteam_public_submit"style="background: #69b754;">
</form>

<!--如果有错,显示后端返回的错误-->
<%if(locals.error){%><td class="wpt_td2"><%=error%><%}%>

?

1
2
3
4
5

function login(req, res, next) {
//如果用户提交了信息,将信息返回到前端页面
var user = req.body.user;
res.render("login", {title:'用户登录', user:user });
};

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

function loginAction(req, res, next) {
var user = req.body.user;
if(!user || !user.mobile || !user.pwd) {
if (req.xhr) {
res.json({ret:-1, errMsg:'手机号或密码不能为空'});
} else {
res.locals.error = '手机号或密码不能为空';
login(req, res, next);
}
} else {
if (user.mobile === '12345678901' && user.pwd === '123456'){
if (req.xhr){
res.json({ret:0,errMsg:"",backUrl:""});
} else {
res.redirect("/home");
}
} else {
//将错误信息放到locals中,然后调用视图函数
res.locals.error = "用户名或密码错误";
login(req, res, next);
}
}
});

‍ res.locals.error = '手机号或密码不能为空';

login(req, res, next);