在jQuery中Html()方法

来源:互联网 发布:java builder模式 编辑:程序博客网 时间:2024/04/29 22:27

在jQuery中用Html()得到的HTML代码并不包含在页面上动态输入的值,如果想要将动态输入的值一起得到,需调用以下一个方法:

(function ($) {                var oldHTML = $.fn.html;                $.fn.formhtml = function () {                    if (arguments.length) return oldHTML.apply(this, arguments);                    $("input,textarea,button", this).each(function () {                        this.setAttribute('value', this.value);                    });                    $(":radio,:checkbox", this).each(function () {                        if (this.checked) this.setAttribute('checked', 'checked');                        else this.removeAttribute('checked');                    });                    $("option", this).each(function () {                        if (this.selected) this.setAttribute('selected', 'selected');                        else this.removeAttribute('selected');                    });                    return oldHTML.apply(this);                };            })(jQuery);

那么如何引用这个方法呢?如下:

var content = $("#txtHtml").formhtml();//其中的txtHtml就是你要获取Html代码的容器ID,然后返回的content的值就包含在页面上动态输入的各个控件所对应的值了



0 0
原创粉丝点击