一,js处理富文本
function decodeHtml(s) {
var HTML_DECODE = {
"<": "<",
">": ">",
"&": "&",
" ": " ",
""": "\"",
"©": ""
// Add more
};
var REGX_HTML_ENCODE = /"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g;
var REGX_HTML_DECODE = /&\w+;|&#(\d+);/g;
var REGX_TRIM = /(^\s*)|(\s*$)/g;
s = (s != undefined) ? s : "";
return (typeof s != "string") ? s :
s.replace(REGX_HTML_DECODE,
function ($0, $1) {
var c = HTML_DECODE[$0];
if (c == undefined) {
// Maybe is Entity Number
if (!isNaN($1)) {
c = String.fromCharCode(($1 == 160) ? 32 : $1);
} else {
c = $0;
}
}
return c;
});
};
$(document).ready(function(){
var content= "{{$data['content']}}";
$("#content").append(decodeHtml(content));
});
二,表单提交
//获取表单的所有数据
var form_data = $('#From_id_2').serializeArray();
var m = [], idata;
$.each(form_data, function (i, field) {
m.push('"' + field.name + '":"' + encodeURI(field.value) + '"');
});
idata = '{' + m.join(',') + '}';
// 按字符 idata 转换成 JSON 格式
idata = eval('(' + idata + ')');
console.log(idata); //表单的所有数据,可以直接提交到后台