测试方法: 将以下代码保存到htm文件放到服务器上。然后将这个html文件的URL发给微信上任意好友后点开访问。
测试效果: 在打开的页面中输入姓名,勾选爱好里面的一项或两项。然后点击扫码,当扫码内容显示到输入框后表单里其他内容并未丢失。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>调用扫码后表单数据不丢失</title>
<link rel="stylesheet" href="//at.alicdn.com/t/font_626784_mcjb2yy4vfl.css" />
<script src="//cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
姓名:<input type="text" id="username"><br>
学号:<input type="text" id="usernum"> <i id="saoma" class='dzwfont dzw-saomiao'></i><br>
爱好:<input type="checkbox" id="chouyan">抽烟 <input type="checkbox" id="hejiu">喝酒<br>
<input type="button" id="tijiao" value="提交">
</body>
<script type="text/javascript">
$(document).ready(function() {
//页面载入时读取之前保存的数据并设置
$("#username").val( localStorage.getItem("username") );
$("#chouyan").prop("checked", localStorage.getItem("chouyan")=="true" );
$("#hejiu").prop("checked", localStorage.getItem("hejiu")=="true" );
var qr = GetQueryString("qrresult");
if (qr) $("#usernum").val(qr);
});
$("#saoma").click(function() {
//跳转前保存数据
localStorage.setItem("username", $("#username").val());
localStorage.setItem("chouyan", $("#chouyan").prop("checked"));
localStorage.setItem("hejiu", $("#hejiu").prop("checked"));
window.open("//996315.com/api/scan/?redirect_uri=" + encodeURIComponent(location.href), "_self");
});
function GetQueryString(name) {
var reg = new RegExp("\\b" + name + "=([^&]*)");
var r = decodeURIComponent(location.href).match(reg);
if (r != null) return r[1];
}
</script>
</html>