Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将地址字段复制到帐单地址字段中_Javascript_Jquery_Html - Fatal编程技术网

Javascript 将地址字段复制到帐单地址字段中

Javascript 将地址字段复制到帐单地址字段中,javascript,jquery,html,Javascript,Jquery,Html,我想根据复选框值将用户地址复制到账单地址字段中。但每次选中复选框时,只有输入字段的最后一个值被复制到“账单地址”部分。不要使用ID,因为它们是动态设置的 Javascript: $(function () { var checkbox = $('#veranstaltungsanmeldung_billing_address'); checkbox.on('click', function(){ if(checkbox.prop('checked')) {

我想根据复选框值将用户地址复制到账单地址字段中。但每次选中复选框时,只有输入字段的最后一个值被复制到“账单地址”部分。不要使用ID,因为它们是动态设置的

Javascript:

$(function () {
    var checkbox = $('#veranstaltungsanmeldung_billing_address'); 

    checkbox.on('click', function(){
        if(checkbox.prop('checked')) {
            var srcValues = [];

            $(".address").each(function(){
                var srcValue = $(this).find('input[data-src]').val();
                srcValues.push(srcValue);
            });

            $.each(srcValues, function(index){
                $('.billing-address').find('input[data-dst]').val(srcValues[index]);
            });
        }
    });
});
HTML:


{%如果定义了validationErrors%}
{{validationErrors}}
{%endif%}
{{“form.anrede”| trans}:{%if-form.vorname.vars.required==true%}*{%endif%}
{{form_小部件(form.anrede{
'attr':{'class':'form control'}
})}}
{{“form.titel”| trans}:{%if form.titel.vars.required==true%}*{%endif%}
{{form_小部件(form.titel{
'attr':{'class':'form control'}
})}}
{{“form.vorname”| trans}:{%if form.vorname.vars.required==true%}*{%endif%}
{{form_小部件(form.vorname{
'attr':{'class':'form control'}
})}}
{{“form.nachname”| trans}:{%if form.nachname.vars.required==true%}*{%endif%}
{{form_小部件(form.nachname{
'attr':{'class':'form control'}
})}}
{{“form.unternehmen”| trans}:{%if form.unternehmen.vars.required==true%}*{%endif%}
{{form_小部件(form.unternehmen{
'attr':{'class':'formcontrol','datasrc':'company'}
})}}
{{“form.funktion”| trans}:{%if form.funktion.vars.required==true%}*{%endif%}
{{form_小部件(form.funktion{
'attr':{'class':'form control'}
})}}
{{“form.strasse”| trans}:{%if form.strasse.vars.required==true%}*{%endif%}
{{form_小部件(form.strasse{
'attr':{'class':'formcontrol','datasrc':'street'}
})}}
{{“form.plz”| trans}:{%if form.postleitzahl.vars.required==true%}*{%endif%}
{{form_小部件(form.postleitzahl{
'attr':{'class':'form control'}
})}}
{{“form.ort”{124; trans}}:{%if-form.ort.vars.required==true%}*{%endif%}
{{form_小部件(form.ort{
'attr':{'class':'form control'}
})}}
{{“form.land”| trans}}:{%if form.land.vars.required==true%}*{%endif%}
{{form_widget(form.land{
'attr':{'class':'form control'}
})}}
{{“form.telefon”| trans}:{%if form.telefon.vars.required==true%}*{%endif%}
{{form_widget(form.telefon{
'attr':{'class':'form control'}
})}}
{{“form.e_-mail”| trans}:{%if-form.e_-mail.vars.required==true%}*{%endif%}
{{form_小部件(form.e_-mail{
'attr':{'class':'form control'}
})}}
{%if location.id!=3481794%}
{{“form.anmerkungen”| trans}:{%if form.anmerkungen.vars.required==true%}*{%endif%}
{{form_widget(form.anmerkungen{
'attr':{'class':'form control'}
})}}
{%endif%}
{{“form.careercenter.billing_occulation.label”{trans}:{%if form.billing_occulation.vars.required==true%}*{%endif%}
{{form_小部件(form.billing_){
'attr':{'class':'form control'}
})}}
{{form.freies_attribute.vars.label}
{{form.freies_attribute.vars.label}
{{form_小部件(form.freies_attribute)}
{{form_小部件(form.billing_address)}{{{“form.billing_address”| trans}{%if-form.billing_address.vars.required==true%}*{%endif%}
{%if location.id==3481794%}
{{“form.fbd_question”| trans}:{%if form.anmerkungen.vars.required==true%}*{%endif%}
{{form_widget(form.anmerkungen{
'attr':{'class':'form control'}
})}}
{%endif%}
女招待
{{“form.unternehmen”| trans}:{%if form.billing_company.vars.required==true%}*{%endif%}
{{form_widget(form.billing_company{
'attr':{'class':'formcontrol','datadst':'company'}
})}}
{{“form.street”| trans}:{%if form.billing_street.vars.required==true%}*{%endif%}
{{form_widget(form.billing_street{
'attr':{'class':'formcontrol','datadst':'street'}
})}}
{{“form.zip”{124; trans}}:{%if-form.billing_-zip.vars.required==true%}*{%endif%}
{{form_小部件(form.billing_zip{
'attr':{'class':'form control'}
})}}
{{“form.place”| trans}}:{%if form.billing_place.vars.required==true%}*{%endif%}
{{form_小部件(form.billing_位置{
'attr':{'class':'form control'}
})}}
{{“form.country”| trans}:{%if form.billing_country.vars.required==true%}*{%endif%}
{{form_小部件(form.billing_country{
'attr':{'class':'form control'}
})}}

如果有人能帮我解决这个问题,那就太好了。

您正在通过
srcValues
数组循环,将
输入[data dst]
值设置到数组循环中

我的意思是,你正在这样做:

var thisIsInputValue;
var array = [1, 2, 3, 4];
array.forEach(function(value) {
    thisIsInputValue = value;
});
如果在数组中循环,并将
thisIsInputValue
设置为数组值,它将指定最后一个值,如下所示:

var thisIsInputValue;

thisIsInputValue = 1;
thisIsInputValue = 2;
thisIsInputValue = 3;
thisIsInputValue = 4;

// At this point, thisIsInputValue will be 4.
我不知道你到底期望什么

编辑:请注意,
thisIsInputValue
是您的
$('.billing address').find('input[data dst]')).val(srcValues[index])。您只需重置
输入[data dst]
的值。不是康卡特
var thisIsInputValue;

thisIsInputValue = 1;
thisIsInputValue = 2;
thisIsInputValue = 3;
thisIsInputValue = 4;

// At this point, thisIsInputValue will be 4.
$(".address").each(function(){
    // Get the initial input
    var input = $(this).find('input[data-src]');
    // get the data-src value (as will be the thing that identifies the other input)
    var inputDataSrc = input.data("src");
    // get the input value
    var inputValue = input.val();

    // Get the billing input with the same data-src value
    var billingInput = $('.billing-address').find('input[data-dst="'+inputDataSrc+'"]');
    // Set the value
    billingInput.val(inputValue);
});