Javascript 将用户选择的单选内容复制到其他表单

Javascript 将用户选择的单选内容复制到其他表单,javascript,jquery,forms,copy,radio-button,Javascript,Jquery,Forms,Copy,Radio Button,我在我的网站上有2+份登记表。它们具有完全相同的字段,因此必须要求用户不断输入其信息变得疯狂 我添加了一个复选框,允许用户单击,它将复制他们的form1结果(使用.val()而不是clone()函数,该函数弊大于利),以及页面上显示的所有其他表单 我在表单中混合了文本、选择下拉列表和单选按钮。我可以获取文本并选择下拉列表复制到其他文本,但我似乎无法对单选按钮进行复制 下面的代码是我如何处理1个文本字段和1个选择框的示例,因为我不想让你们陷入所有代码的泥潭 $('#sameaddress').cl

我在我的网站上有2+份登记表。它们具有完全相同的字段,因此必须要求用户不断输入其信息变得疯狂

我添加了一个复选框,允许用户单击,它将复制他们的form1结果(使用.val()而不是clone()函数,该函数弊大于利),以及页面上显示的所有其他表单

我在表单中混合了文本、选择下拉列表和单选按钮。我可以获取文本并选择下拉列表复制到其他文本,但我似乎无法对单选按钮进行复制

下面的代码是我如何处理1个文本字段和1个选择框的示例,因为我不想让你们陷入所有代码的泥潭

$('#sameaddress').click(function () {
    if ($('#sameaddress').attr('checked')) {
        //Text Field
        $('#commerce-checkout-form-registration .registration-form .field-name-field-student-id input').val($('#commerce-checkout-form-registration .registration-form-first .field-name-field-student-id input').val());
        //Select box
        var jobtype = $('#commerce-checkout-form-registration .registration-form-first .field-name-field-job-type option:selected').val();
        $('#commerce-checkout-form-registration .registration-form .field-name-field-job-type option[value=' + jobtype + ']').attr('selected', 'selected');
    };
});         
所以你可以看到我是如何做到这一点的,但我正在寻找一些帮助来做同样的收音机选择。有什么想法吗?如果我能保持原来的功能,那就太好了,因为我有大约3-4个无线电问题要拷贝,而且一定有办法

更新 添加一个更大的代码块,因为我认为这可能有助于使这更有意义(是的,这段代码确实有效)。同样,我尝试使用与常规文本输入相同的精确格式来定位单选按钮,但没有成功

// First we need to give the forms some classes we can actually work with.
$("#commerce-checkout-form-registration .registration_information fieldset").addClass("registration-form");
$("#commerce-checkout-form-registration .registration_information fieldset:first").removeClass("registration-form").addClass("registration-form-first");

// Now I add in the check box to use, doing this hear so I can specify that I want it to only show on the first form
$('<div><label for="sameaddress">Use Same Information on additional Registration forms (If applicable)</label><input type="checkbox" name="sameaddress" id="sameaddress" /></div>').insertAfter('.registration_information fieldset:first .field-name-field-how-did-you-hear-about-thi');

// Now lets copy everything to other forms
$('#sameaddress').click(function(){
if($('#sameaddress').attr('checked')){
    $('#commerce-checkout-form-registration .registration-form .field-name-field-student-id input').val($('#commerce-checkout-form-registration .registration-form-first .field-name-field-student-id input').val());
    $('#commerce-checkout-form-registration .registration-form .field-name-field-first-name input').val($('#commerce-checkout-form-registration .registration-form-first .field-name-field-first-name input').val());

    // Copy each line as shown above for each of your text inputs, replacing the parent .field-name-field value for each.

    // For the select boxes see below
    var prefix = $('#commerce-checkout-form-registration .registration-form-first .field-name-field-prefix option:selected').val();
               $('#commerce-checkout-form-registration .registration-form .field-name-field-prefix option[value=' + prefix + ']').attr('selected','selected');
    var jobtype = $('#commerce-checkout-form-registration .registration-form-first .field-name-field-job-type option:selected').val();
                      $('#commerce-checkout-form-registration .registration-form .field-name-field-job-type option[value=' + jobtype + ']').attr('selected','selected');    

} else { 

    //Clear on uncheck

    $('#commerce-checkout-form-registration .registration-form .field-name-field-student-id input').val("");
    $('#commerce-checkout-form-registration .registration-form .field-name-field-first-name input').val("");
    $('#commerce-checkout-form-registration .registration-form .field-name-field-prefix option[value=Nothing]').attr('selected','selected');
    $('#commerce-checkout-form-registration .registration-form .field-name-field-job-type option[value=Nothing]').attr('selected','selected');
};

});
//首先,我们需要为表单提供一些可以实际使用的类。
$(“#商业结算表注册。注册信息字段集”).addClass(“注册表”);
$(“#商业结算表注册。注册信息字段集:第一”).removeClass(“注册表”).addClass(“注册表第一”);
//现在我添加了要使用的复选框,这样我就可以指定只在第一个表单上显示它
$('Use Same Information on additional Registration forms(如果适用)')。insertAfter('Registration_Information fieldset:first.field name field您是如何得知此信息的?);
//现在让我们将所有内容复制到其他表单
$('#sameaddress')。单击(函数(){
if($('#sameaddress').attr('checked')){
$(“#商业结帐表注册。注册表。字段名字段学生id输入”).val($(“#商业结帐表注册。先注册表。字段名字段学生id输入”).val();
$(“#商业结帐表注册.注册表.字段名字段名输入”).val($(“#商业结帐表注册.注册表.字段名字段名输入”).val());
//如上图所示,复制每个文本输入的每一行,替换每个文本输入的父.field name字段值。
//有关选择框,请参见下文
var prefix=$('#商业结算表单注册。先注册表单。字段名字段前缀选项:选定')。val();
$(“#商业结帐表单注册.注册表单.字段名字段前缀选项[值=”+前缀+”).attr('selected','selected');
var jobtype=$('#商业结算表单注册。先注册表单。字段名称字段作业类型选项:选中')。val();
$(“#商业结帐表单注册。注册表单。字段名称字段作业类型选项[value='+jobtype+'])。attr('selected','selected');
}否则{
//取消选中时清除
$(“#商业结帐表注册表.注册表.字段名字段学生id输入”).val(“”);
$(“#商业结算表注册。注册表。字段名字段名输入”).val(“”);
$(“#商业结帐表单注册。注册表单。字段名字段前缀选项[value=Nothing]”)。attr('selected','selected');
$(“#商业结帐表单注册。注册表单。字段名称字段作业类型选项[value=Nothing]”)。attr('selected','selected');
};
});

呃,我继续,只是改变了单选按钮,选择了可以正常工作的列表

这是个老问题,但也许有人需要解决,就像我一样

假设你有:

<input type="radio" name="myRadioButton" value="a" />Option One<br />
<input type="radio" name="myRadioButton" value="b" />Option Two<br />
<input type="radio" name="myRadioButton" value="c" />Option Three<br />
例如,要设置第二个值,请使用:

$('input[name="myRadioButton"]:checked').val();
$('input[name="myRadioButton"][value="b"]').prop('checked', true);
如果有两个单选按钮,它们的值完全相同,如下所示:

<input type="radio" name="firstRadio" value="a" />Option One<br />
<input type="radio" name="firstRadio" value="b" />Option Two<br />
<input type="radio" name="firstRadio" value="c" />Option Three<br />

<input type="radio" name="secondRadio" value="a" />Option One<br />
<input type="radio" name="secondRadio" value="b" />Option Two<br />
<input type="radio" name="secondRadio" value="c" />Option Three<br />
$('input[name="firstRadio"][value="'+$('input[name="secondRadio"]:checked').val()+'"]')
    .prop('checked', true);

这会将所选选项从secondRadio复制到firstRadio。

对于radio,您可以使用
val()
执行与文本相同的操作。。但是你确定这个代码有效吗。。因为我怀疑…有人吗@bipen的建议不起作用。是的,脚本运行良好,请参阅原始帖子中的更新。