简体   繁体   English

单选按钮和文本区域的jquery .clone()值

[英]jquery .clone() values for radio buttons and text areas

I know this can be done because I was reading up on it. 我知道可以这样做,因为我正在阅读。 But, I can't get mine to work. 但是,我不能让我上班。 At the minute my inputs all clone which is all good. 在此刻,我的输入全部克隆,这一切都很好。 However this is the crucial part. 但这是关键部分。 I need to keep all the checked radio buttons etc currently. 我需要保留所有选中的单选按钮等。 It just acts, as if it is brand new and has not been filled. 它只是表现得好像是全新的,尚未被填充。 I am not quite sure how to achieve. 我不太确定如何实现。 This my code is below. 这是我的代码如下。 Any help would be really appreciated. 任何帮助将非常感激。

var k = jQuery('.c_questions');
for(var j = 0; j < k.length; j++ ){
    var storeAll = jQuery(k[j]);
    jQuery(storeAll).clone().appendTo('.payment');
}

The checked state of form controls are not part of the DOM (this is also true of selected and the current rather than default value ), so cloning doesn't copy them. 表单控件的checked状态不是DOM的一部分(对于selected当前 value ,而不是默认value也是如此),因此克隆不会复制它们。 But the good news is you can copy them yourself by looping through the clones and setting their checked state to match the original's checked state: 但是好消息是,您可以通过循环复制克隆并将它们的checked状态设置为与原始checked状态匹配来自己复制它们:

var k = jQuery('.c_questions');
for (var j = 0; j < k.length; j++ ){
    var storeAll = jQuery(k[j]);
    var originals = jQuery(storeAll); // See comment on question, this call is suspect
    var clones = originals.clone();
    var cloneRadios = clones.find("input[type=radio]");
    originals.find("input[type=radio]").each(function(index) {
        cloneRadios.eq(index).prop("checked", this.checked);
    });
    clones.appendTo('.payment');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM