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

后端 未结 7 629
礼貌的吻别
礼貌的吻别 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:56

    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'

提交回复
热议问题