少说多做,直接运行代码,代码中有注释:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>serialize 序列化 +号处理</title>
</head>
<body>
<form id="form">
<label>
<input type="checkbox" name="r1" id="r1" value="前端工程师 厉害!" />前端工程师
</label>
<label>
<input type="checkbox" name="r1" id="r1" value="测试工程师 厉害!" />测试工程师
</label>
<label>
<input type="checkbox" name="r1" id="r1" value="后端工程师 厉害!" />后端工程师
</label>
<input type="button" name="btn" id="btn" value="提交" />
</form>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
//serialize序列化,默认会调用encodeURIComponent()进行编码
var pre = $('#form').serialize()
//r1=%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%B8%88++%E5%8E%89%E5%AE%B3%EF%BC%81
//注意有+号
console.log(pre);
//对整个字符串中符合条件的+号都进行替换
var next = pre.replace(/\+/g," ");
//对serialize后的内容进行解码
next = decodeURIComponent(next);
//r1=前端工程师 厉害!
//注意没有+号了
console.log(next)
});
});
</script>
</body>
</html>