jquery的clone无法成功复制textarea和select
原址:http://stackoverflow.com/questions/742810/clone-isnt-cloning-select-values
https://github.com/spencertipping/jquery.fix.clone
(function (original) {
jQuery.fn.clone = function () {
var result = original.apply(this, arguments),
my_textareas = this.find('textarea').add(this.filter('textarea')),
result_textareas = result.find('textarea').add(result.filter('textarea')),
my_selects = this.find('select').add(this.filter('select')),
result_selects = result.find('select').add(result.filter('select'));
for (var i = 0, l = my_textareas.length; i < l; ++i) $(result_textareas[i]).val($(my_textareas[i]).val());
for (var i = 0, l = my_selects.length; i < l; ++i) {
for (var j = 0, m = my_selects[i].options.length; j < m; ++j) {
if (my_selects[i].options[j].selected === true) {
result_selects[i].options[j].selected = true;
}
}
}
return result;
};
}) (jQuery.fn.clone);
直接在需要的页面引用这个js即可
本文为Phil Jing原创文章,未经博主允许不得转载,如有问题请直接回复或者加群。