Home  >  Article  >  Web Front-end  >  Detailed explanation of js rich text processing and form submission function examples

Detailed explanation of js rich text processing and form submission function examples

小云云
小云云Original
2018-03-13 15:32:542525browse

本文主要和大家分享js富文本处理和表单提交功能实例详解,主要以代码的形式和大家分享,希望能帮助到大家。

一,js处理富文本

 function decodeHtml(s) {
        var HTML_DECODE = {
            "<": "<",
            ">": ">",
            "&": "&",
            " ": " ",
            """: "\"",
            "&copy;": ""
            // Add more
        };

        var REGX_HTML_ENCODE = /"|&|&#39;|<|>|[\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[&#39;content&#39;]}}";
        $("#content").append(decodeHtml(content));
    });

二,表单提交

        //获取表单的所有数据
       var form_data = $(&#39;#From_id_2&#39;).serializeArray();
     
        var m = [], idata;

        $.each(form_data, function (i, field) {
            m.push(&#39;"&#39; + field.name + &#39;":"&#39; + encodeURI(field.value) + &#39;"&#39;);
        });
        idata = &#39;{&#39; + m.join(&#39;,&#39;) + &#39;}&#39;;
        // 按字符 idata 转换成 JSON 格式
        idata = eval(&#39;(&#39; + idata + &#39;)&#39;);
        console.log(idata);  //表单的所有数据,可以直接提交到后台

相关推荐:

简单实现JavaScript 富文本编辑器的方法

The above is the detailed content of Detailed explanation of js rich text processing and form submission function examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn