JS使用html()獲取html代碼獲取不到input、textarea控件填寫的值


 

 

我們可以重寫一個方法

(function ($) {
        var oldHTML = $.fn.html;
        $.fn.formhtml = function () {
            if (arguments.length) return oldHTML.apply(this, arguments);
            $("input,button", this).each(function () {
                this.setAttribute('value', this.value);
            });
            $("textarea", this).each(function () {
                this.setAttribute('value', this.value);
                this.innerHTML= this.value
            });
            $(":radio,:checkbox", this).each(function () {
                // im not really even sure you need to do this for "checked"
                // but what the heck, better safe than sorry
                if (this.checked) this.setAttribute('checked', 'checked');
                else this.removeAttribute('checked');
            });
            $("option", this).each(function () {
                // also not sure, but, better safe...
                if (this.selected) this.setAttribute('selected', 'selected');
                else this.removeAttribute('selected');
            });
            return oldHTML.apply(this);
        };
       
    })(jQuery);

 

調用的使用 

var html=$("#id").formhtml();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM