Is there a way to clone form field values in jQuery or javascript?

后端 未结 7 667
礼貌的吻别
礼貌的吻别 2020-11-28 14:01

jQuery has a clone() function that clones the actual form with no problem, but it doesn\'t preserve any values that have been entered into the form.

Is

7条回答
  •  天涯浪人
    2020-11-28 14:45

    You may use this jQuery Plugin.

    /**
     * clone element, including the textarea part
     */
    
    
    $.fn.clone2 = function(val1, val2) {
        // ret for return value, cur for current jquery object
        var ret, cur;
        ret = $(this).clone(val1, val2);
        cur = $(this);
    
        // copy all value of textarea
        ret.find('textarea').each(function() {
            var value_baru;
    
            // use name attribute as unique id
            value_baru = sek.find('[name="$name"]'.replace('$name', $(this).attr('name')))
                            .val();
    
            // set the new value to the textarea
            $(this).val(value_baru);
        });
    
        // return val
        return ret;
    }
    

提交回复
热议问题