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

為{幸葍}努か 提交于 2019-11-26 08:29:20

问题


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 there a way to get around this?

Sample code would be much appreciated.


回答1:


ran into the same problem, simple solution:

// touch all input values
$('input:text').each(function() {
    $(this).attr('value', $(this).val());
});

var clones = $('input:text').clone();

the trick is that this will change the actual 'value' attribute in the DOM tree, otherwise the data you enter 'on-the-fly' only exists in the DOM 'state'




回答2:


Stemming from the notes, here's a solution. With the following form:

<form id="old">
    <textarea>Some Value</textarea>
    <input type="text" value="Some Value" />
    <input type="checkbox" value="bob" checked />
    <br />
</form>

<input type="button" value="Clone" id="clone" />

This jQuery works, including the textareas:

$( 'input#clone' ).click(
    function()
    {
      $( 'form#old textarea' ).text( $( 'form#old textarea' ).val() )
      $("form#old").clone().attr( 'id', 'new_form' ).appendTo("body")

    }
)

​Demo: http://jsfiddle.net/Jux3e/




回答3:


Another easy fix for the textarea values not being cloned is to include the following JavaScript file in your HTML: https://github.com/spencertipping/jquery.fix.clone

It just patches the clone method so you can include the file and then forget it's there. Apparently there is a problem with cloneing select values too and this same file fixes that problem as well.

This file was linked to from: http://bugs.jquery.com/ticket/3016. The bug is 4 years old.




回答4:


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;
}



回答5:


if you need to duplicate the field itself, check this tinny function relCopy




回答6:


Found this problem and wrote this wrapper:

$.fn.cloneField = function(withDataAndEvents) {
var clones = [];
this.each(function(){
    var cln = $(this).clone(withDataAndEvents);
    cln.val($(this).val());
    clones.push(cln.get(0));
});
return jQuery( clones ); }; 



回答7:


Use this code to copy textarea values

clonedObject.find(textareaObject).val(originalObject.find(textareaObject).val());


来源:https://stackoverflow.com/questions/3250685/is-there-a-way-to-clone-form-field-values-in-jquery-or-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!