Jquery's clone method applies to bug fixes for textarea and select

  • 2020-03-30 03:29:14
  • OfStack

Test found that there is a problem with the clone method of jquery of textarea and select, the value of textarea and select will be lost when clone, found that this is a bug of jquery, can not on the code, relatively simple. Just re-assign val when you have clone, if you know this, you can simply write it yourself.

Introduce the clone page you want to use

Jquery. Fix. Clone. Js


(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) result_selects[i].selectedIndex = my_selects[i].selectedIndex;

return result;
};
}) (jQuery.fn.clone);

Related articles: