innerHTML with current form values

前端 未结 5 1733
太阳男子
太阳男子 2020-12-17 01:48

Need .innerHTML functionality but with the current form field values including input, select (selected option), and textarea.

So, given:

5条回答
  •  遥遥无期
    2020-12-17 02:12

    /**
     * HTML content along with the values entered by the user for form elements
     * (unlike the default browser behavior)
     *
     * @author i.carter euona.com
     * @version 1.0 2012-04-14
     * @return innerHTML (or outerHTML)
     * @param e HTMLElement
     * @param t String: "outer" OR "inner" (default)
     * 
     * @effect changes defaultValue
     * @tested FF11,IE10,GC
     */
    function getHTML(e,t)
    {   
        if(typeof t=='undefined') var t='inner';
        switch(e.nodeName.toUpperCase())
        {
            case 'INPUT':
                if(e.type=='checkbox' || e.type=='radio')
                {
                    e.defaultChecked=e.checked;
                    break;
                }
            case 'TEXTAREA':
                e.defaultValue=e.value;
                break;
            case 'SELECT':
                var o=e.options,i=o.length;
                while(--i > -1)
                    o[i].defaultSelected=o[i].selected;
                break;
            default:
                var x=e.getElementsByTagName('input'),i=x.length;
                while(--i > -1) getHTML(x[i],t);
                x=e.getElementsByTagName('textarea');i=x.length;
                while(--i > -1) getHTML(x[i],t);
                x=e.getElementsByTagName('select');i=x.length;
                while(--i > -1) getHTML(x[i],t);
        }
        return e[t+'HTML'];
    }
    

提交回复
热议问题