有没有办法在jQuery或JavaScript中克隆表单字段值? [英] Is there a way to clone form field values in jQuery or javascript?

查看:113
本文介绍了有没有办法在jQuery或JavaScript中克隆表单字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery有一个 clone()函数,可以毫无问题地克隆实际的表单,但它不会保留任何已经输入表单的值。

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?

示例代码将非常感谢。

推荐答案

遇到同样的问题,简单的解决方案:

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();

诀窍是这会改变DOM树中实际的'value'属性,否则数据你输入'即时'只存在于DOM'状态'

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'

这篇关于有没有办法在jQuery或JavaScript中克隆表单字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆