2015-01-20 2 views
0

이 양식을 동적으로 만드는 데 어려움을 겪고 있습니다. 나는 고객이 1에서 5까지 선택할 수있는 루프를 가지고 있는데, 사용자가 그 루프 수를 선택했다. _<cfoutput>#Add#</cfoutput>을 추가 할 필요가있는 곳을 알아 내기 위해 노력하고 있습니다. 따라서 루프가 반복 될 때 번호가 추가되고 올바르게 작동합니다. 루프가 작동 중이며 "1 Loop"가 선택되면 양식이 작동합니다. 두 번째 루프를 만들기 위해 JavaScript에 _<cfoutput>#Add#</cfoutput>을 추가 할 위치가 확실치 않아서 새 이름과 ID를 수신 할 수 있도록 올바르게 작동합니다. 내가 시도한 모든 것은 자바 스크립트를 깨뜨린 것이다. 제안과 조언은 크게 감사하겠습니다! 루프의ColdFusion/Javascript 동적 양식

외관
http://jsfiddle.net/bobrierton/gettgpmj/5/ (위치 정보에 대한 자바 스크립트를 반복하지 않음)

enter image description here

var placeSearch, autocomplete, autocomplete2; 
 
var componentForm = { 
 
    route: 'long_name', 
 
    locality: 'long_name', 
 
    administrative_area_level_1: 'short_name', 
 
    postal_code: 'short_name' 
 
}; 
 
var componentForm2 = { 
 
    route2: 'long_name', 
 
    locality2: 'long_name', 
 
    administrative_area_level_12: 'short_name', 
 
    postal_code2: 'short_name' 
 
}; 
 

 
function initialize() { 
 
    // Create the autocomplete object, restricting the search 
 
    // to geographical location types. 
 
    autocomplete = new google.maps.places.Autocomplete(
 
    /** @type {HTMLInputElement} */ 
 
    (document.getElementById('autocomplete')), { 
 
     types: ['geocode'] 
 
    }); 
 
    autocomplete2 = new google.maps.places.Autocomplete(
 
    /** @type {HTMLInputElement} */ 
 
    (document.getElementById('autocomplete2')), { 
 
     types: ['geocode'] 
 
    }); 
 
    // When the user selects an address from the dropdown, 
 
    // populate the address fields in the form. 
 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
 
    fillInAddress(); 
 
    }); 
 
    google.maps.event.addListener(autocomplete2, 'place_changed', function() { 
 
    fillInAddress2(); 
 
    }); 
 
} 
 

 
// [START region_fillform] 
 
function fillInAddress() { 
 
    // Get the place details from the autocomplete object. 
 
    var place = autocomplete.getPlace(); 
 

 
    for (var component in componentForm) { 
 
    document.getElementById(component).value = ''; 
 
    document.getElementById(component).disabled = false; 
 
    } 
 

 
    // Get each component of the address from the place details 
 
    // and fill the corresponding field on the form. 
 
    for (var i = 0; i < place.address_components.length; i++) { 
 
    var addressType = place.address_components[i].types[0]; 
 
    if (componentForm[addressType]) { 
 
     var val = place.address_components[i][componentForm[addressType]]; 
 
     document.getElementById(addressType).value = val; 
 
    } 
 
    } 
 
    //var keys=[];for (var key in place.address_components[0]) keys.push(key); 
 
    //alert(keys): 
 
    document.getElementById('autocomplete').value = 
 
    place.address_components[0]['long_name'] + ' ' + 
 
    place.address_components[1]['long_name']; 
 
    
 
    /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/ 
 
    document.getElementById('route').value = ''; 
 
} 
 

 
function fillInAddress2() { 
 
    // Get the place details from the autocomplete object. 
 
    var place = autocomplete2.getPlace(); 
 

 
    for (var component in componentForm2) { 
 

 
     document.getElementById(component).value = ''; 
 
     document.getElementById(component).disabled = false; 
 
    } 
 

 
    // Get each component of the address from the place details 
 
    // and fill the corresponding field on the form. 
 
    for (var i = 0; i < place.address_components.length; i++) { 
 
     var addressType = place.address_components[i].types[0]; 
 
     if (componentForm2[addressType + '2']) { 
 
     var val = place.address_components[i][componentForm2[addressType + '2']]; 
 
     document.getElementById(addressType + '2').value = val; 
 
     } 
 
    } 
 
    document.getElementById('autocomplete2').value = 
 
    place.address_components[0]['long_name'] + ' ' + 
 
    place.address_components[1]['long_name']; 
 
    
 
    /*document.getElementById('route2').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete2').value : '');*/ 
 
    document.getElementById('route2').value = ''; 
 
} 
 
    // [END region_fillform] 
 

 
// [START region_geolocation] 
 
// Bias the autocomplete object to the user's geographical location, 
 
// as supplied by the browser's 'navigator.geolocation' object. 
 
function geolocate() { 
 
    if (navigator.geolocation) { 
 
    navigator.geolocation.getCurrentPosition(function(position) { 
 
     var geolocation = new google.maps.LatLng(
 
     position.coords.latitude, position.coords.longitude); 
 
     var circle = new google.maps.Circle({ 
 
     center: geolocation, 
 
     radius: position.coords.accuracy 
 
     }); 
 
     autocomplete.setBounds(circle.getBounds()); 
 
    }); 
 
    } 
 
} 
 

 
function geolocate2() { 
 
    if (navigator.geolocation) { 
 
     navigator.geolocation.getCurrentPosition(function(position) { 
 
     var geolocation = new google.maps.LatLng(
 
      position.coords.latitude, position.coords.longitude); 
 
     var circle = new google.maps.Circle({ 
 
      center: geolocation, 
 
      radius: position.coords.accuracy 
 
     }); 
 
     autocomplete2.setBounds(circle.getBounds()); 
 
     }); 
 
    } 
 
    } 
 
    // [END region_geolocation] 
 
initialize(); 
 

 
document.querySelector('#chbSame').addEventListener('change', checkedAddr); 
 

 

 
function checkedAddr() { 
 
    if (document.getElementById('chbSame').checked) { 
 
    document.getElementById('route2').value = document.getElementById('route').value; 
 
    document.getElementById('locality2').value = document.getElementById('locality').value; 
 
    document.getElementById('administrative_area_level_12').value = document.getElementById('administrative_area_level_1').value; 
 
    document.getElementById('postal_code2').value = document.getElementById('postal_code').value; 
 
    
 
document.getElementById('autocomplete2').value = document.getElementById('autocomplete').value; 
 
    } else { 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script> 
 

 
<body onload="initialize()"> 
 
<div class="clearfix"> 
 
\t <label for="street_1">Mailing Address 1:</label> 
 
\t <input name="street_1" type="text" maxlength="120" onfocus="geolocate()" id="autocomplete" size="54" /> 
 
</div> 
 

 
<div class="clearfix"> 
 
\t <label for="m2street_1">Mailing Address 2:</label> 
 
\t <input name="m2street_1" type="text" maxlength="120" size="54" id="route" /> 
 
</div> 
 
     
 
<div class="clearfix"> 
 
\t <label for="city_1">City:</label> 
 
\t <input name="city_1" type="text" maxlength="50" size="30" id="locality" /> 
 
    
 
<div class="clearfix"> 
 
    \t <label for="state_1">State:</label> 
 
    \t <input type="text" name="state_1" id="administrative_area_level_1" size="8" maxlength="12"> 
 
    </div> 
 

 
     
 
<div class="clearfix"> 
 
\t <label for="street_1">Zip Code:</label> 
 
\t <input name="postal_1" type="text" maxlength="12" size="8" id="postal_code" /> 
 
</div> 
 
      
 
     <script type="text/javascript"> 
 
     function FillBilling_1(f) { 
 
    if(f.billingtoo_1.checked == true) { 
 
    f.pstreet_1.value = f.street_1.value; 
 
\t f.p2street_1.value = f.m2street_1.value; 
 
    f.pcity_1.value = f.city_1.value; 
 
\t f.pstate_1.value = f.state_1.value; 
 
\t f.ppostal_1.value = f.postal_1.value; 
 
    } 
 
} 
 
     </script> 
 
     
 
<div class="clearfix"> 
 
\t <input type="checkbox" name="billingtoo_1" onclick="FillBilling_1(this.form)" id="chbSame"> 
 
\t <em>Check this box if Physical Address and Mailing Address are the same.</em> 
 
</div> 
 

 

 
     
 
<div class="clearfix"> 
 
\t <label for="pstreet_1">Physical Address 1:</label> 
 
\t <input name="pstreet_1" type="text" maxlength="120" onfocus="geolocate2()" id="autocomplete2" size="53" /> 
 
</div> 
 

 
<div class="clearfix"> 
 
\t <label for="p2street_1">Physical Address 2:</label> 
 
\t <input name="p2street_1" type="text" maxlength="120" size="53" id="route2" /> 
 
</div> 
 
     
 
<div class="clearfix"> 
 
\t <label for="pcity_1">City:</label> 
 
\t <input name="pcity_1" type="text" maxlength="50" size="30" id="locality2" /> 
 
    
 
<div class="clearfix"> 
 
    \t <label for="pstate_1">State:</label> 
 
    \t <input type="text" name="pstate_1" id="administrative_area_level_12" size="8" maxlength="12"> 
 
    </div> 
 

 
     
 
<div class="clearfix"> 
 
\t <label for="pstreet_1">Zip Code:</label> 
 
\t <input name="ppostal_1" type="text" maxlength="12" size="8" id="postal_code2" /> 
 
</div> 
 
<br> 
 
    <br> 
 
      
 
<div class="clearfix"> 
 
\t <label for="street_2">Mailing Address 1:</label> 
 
\t <input name="street_2" type="text" maxlength="120" onfocus="geolocate()" id="autocomplete" size="54" /> 
 
</div> 
 

 
<div class="clearfix"> 
 
\t <label for="m2street_2">Mailing Address 2:</label> 
 
\t <input name="m2street_2" type="text" maxlength="120" size="54" id="route" /> 
 
</div> 
 
     
 
<div class="clearfix"> 
 
\t <label for="city_2">City:</label> 
 
\t <input name="city_2" type="text" maxlength="50" size="30" id="locality" /> 
 
    
 
    <div class="clearfix"> 
 
    \t <label for="state_2">State:</label> 
 
    \t <input type="text" name="state_2" id="administrative_area_level_1" size="8" maxlength="12"> 
 
    </div> 
 
     <div class="clearfix"> 
 
\t <label for="street_2">Zip Code:</label> 
 
\t <input name="postal_2" type="text" maxlength="12" size="8" id="postal_code" /> 
 
</div> 
 
      
 
     <script type="text/javascript"> 
 
     function FillBilling_2(f) { 
 
    if(f.billingtoo_2.checked == true) { 
 
    f.pstreet_2.value = f.street_2.value; 
 
\t f.p2street_2.value = f.m2street_2.value; 
 
    f.pcity_2.value = f.city_2.value; 
 
\t f.pstate_2.value = f.state_2.value; 
 
\t f.ppostal_2.value = f.postal_2.value; 
 
    } 
 
} 
 
     </script> 
 
     
 
<div class="clearfix"> 
 
\t <input type="checkbox" name="billingtoo_2" onclick="FillBilling_2(this.form)" id="chbSame"> 
 
\t <em>Check this box if Physical Address and Mailing Address are the same.</em> 
 
</div> 
 

 

 
     
 
<div class="clearfix"> 
 
\t <label for="pstreet_2">Physical Address 1:</label> 
 
\t <input name="pstreet_2" type="text" maxlength="120" onfocus="geolocate2()" id="autocomplete2" size="53" /> 
 
</div> 
 

 
<div class="clearfix"> 
 
\t <label for="p2street_2">Physical Address 2:</label> 
 
\t <input name="p2street_2" type="text" maxlength="120" size="53" id="route2" /> 
 
</div> 
 
     
 
<div class="clearfix"> 
 
\t <label for="pcity_2">City:</label> 
 
\t <input name="pcity_2" type="text" maxlength="50" size="30" id="locality2" /> 
 
    
 
    <div class="clearfix"> 
 
    \t <label for="pstate_2">State:</label> 
 
    \t <input type="text" name="pstate_2" id="administrative_area_level_12" size="8" maxlength="12"> 
 
    </div> 
 
     
 
     <div class="clearfix"> 
 
\t <label for="pstreet_2">Zip Code:</label> 
 
\t <input name="ppostal_2" type="text" maxlength="12" size="8" id="postal_code2" /> 
 
</div>​

+1

루프가 어디에 있습니까? 어디에서 '추가'를 만들거나 업데이트합니까? – duncan

+2

제 생각에는 당면한 문제를 다루는 데 필요한 코드만으로 작업하는 것이 좋습니다. 알아 낸 것이 있으면 응용 프로그램에 솔루션을 통합 할 수 있습니다. –

답변

2

루프를 사용하여 생성되는 요소에 상수를 id 부여하면 좋지 않습니다. 아래 스 니펫에서 autocompleteautocomplete2 요소에 대한 고유 한 id을 생성했습니다 (autocomplete2auto2complete으로 바꿨습니다). 각 자동 완성 요소를 초기화하기 위해 배열이 사용됩니다. 스크립트가 <cfoutput> 내에 중첩되는 경우 <script> jQuery에 ID를 선택 #을 사용할 수 없습니다

<cfoutput> 
    <script> 
     var totalLoops = #add#; 
     /*rest of the code*/ 
    </script> 
</cfoutput> 

참고 :

지금 자바 스크립트를 사용하여 ColdFusion 변수를 루프를 들어, 같은 totalLoops을 설정합니다.

var totalLoops=5; 
 
    for (i = 0; i < totalLoops; i++) {   
 
     var myHtml = '<div class="clearfix">' 
 
      + ' <label for="street_' + i + '">Mailing Address 1 ' + i + ':</label>' 
 
      + ' <input type="text" id="autocomplete'+i+'" name="street_'+i+'" onFocus="geolocate()" value="">' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
    \t  + ' <label for="m2street_'+i+'">Mailing Address 2 '+i+':</label>' 
 
    \t  + ' <input type="text" name="m2street_'+i+'" id="route'+i+'" value="">' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
    \t  + ' <label for="city_'+i+'">City: '+i+':</label>' 
 
    \t  + ' <input type="text" name="city_'+i+'" id="locality'+i+'" value="">' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
    \t  + ' <label for="state_'+i+'">State: '+i+':</label>' 
 
    \t  + ' <input type="text" name="state_'+i+'" id="administrative_area_level_1'+i+'" value="">' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
      + ' <label for="postal_'+i+'">Zip Code: '+i+':</label>' 
 
    \t  + ' <input type="text" name="postal_'+i+'" id="postal_code'+i+'" value="">' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
    \t  + ' <input type="checkbox" name="billingtoo_'+i+'" id="chbSame" onclick="FillBilling_'+i+'(this.form)">' 
 
      + '<em>Check this box if Physical Address and Mailing Address are the same.</em>' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
      + ' <label for="pstreet_' + i + '">Physical Address 1 ' + i + ':</label>' 
 
      + ' <input type="text" id="auto2complete'+i+'" name="pstreet_'+i+'" onFocus="geolocate2()" value="">' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
    \t  + ' <label for="p2street_'+i+'">Physical Address 2 '+i+':</label>' 
 
    \t  + ' <input type="text" name="p2street_'+i+'" id="route2'+i+'" value="">' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
    \t  + ' <label for="pcity_'+i+'">City: '+i+':</label>' 
 
    \t  + ' <input type="text" name="pcity_'+i+'" id="locality2'+i+'" value="">' 
 
      + '<div class="clearfix">' 
 
    \t  + ' <label for="pstate_'+i+'">State: '+i+':</label>' 
 
    \t  + ' <input type="text" name="pstate_'+i+'" id="administrative_area_level_12'+i+'" value="">' 
 
      + '</div>' 
 
      + '</div>' 
 
      + '<div class="clearfix">' 
 
      + ' <label for="ppostal_'+i+'">Zip Code: '+i+':</label>' 
 
    \t  + ' <input type="text" name="ppostal_'+i+'" id="postal_code2'+i+'" value="">' 
 
      + '</div>' 
 
      + '<br />'; 
 
     $('body').append(myHtml); 
 
    } 
 

 
var placeSearch; 
 
var autocomplete = new Array(); 
 
var auto2complete = new Array(); 
 
var componentForm = new Array(); 
 
var componentForm2 = new Array(); 
 
for (i = 0; i < totalLoops; i++) { 
 
    componentForm[i] = { 
 
     route: 'long_name', 
 
     locality: 'long_name', 
 
     administrative_area_level_1: 'short_name', 
 
     postal_code: 'short_name' 
 
    }; 
 

 
    componentForm2[i] = { 
 
     route2: 'long_name', 
 
     locality2: 'long_name', 
 
     administrative_area_level_12: 'short_name', 
 
     postal_code2: 'short_name' 
 
    }; 
 
} 
 

 
function initialize() { 
 
    // Create the autocomplete object, restricting the search 
 
    // to geographical location types. 
 
    for (i = 0; i < totalLoops; i++) { 
 
    \t (function(i){ 
 
\t  autocomplete[i] = new google.maps.places.Autocomplete(
 
\t  /** @type {HTMLInputElement} */ 
 
\t  (document.getElementById('autocomplete'+i)), { 
 
\t  types: ['geocode'] 
 
\t  }); 
 
\t  auto2complete[i] = new google.maps.places.Autocomplete(
 
\t  /** @type {HTMLInputElement} */ 
 
\t  (document.getElementById('auto2complete'+i)), { 
 
\t  types: ['geocode'] 
 
\t  }); 
 
\t // When the user selects an address from the dropdown, 
 
\t // populate the address fields in the form. 
 
\t google.maps.event.addListener(autocomplete[i], 'place_changed', function() { 
 
\t  fillInAddress(i); 
 
\t }); 
 
\t google.maps.event.addListener(auto2complete[i], 'place_changed', function() { 
 
\t  fillInAddress2(i); 
 
\t }); 
 
\t }(i)); 
 
    } 
 
} 
 

 
// [START region_fillform] 
 
function fillInAddress(idx) { 
 
    // Get the place details from the autocomplete object. 
 
    
 
    var place = autocomplete[idx].getPlace(); 
 

 
    for (var component in componentForm[idx]) { 
 
    document.getElementById(component+idx).value = ''; 
 
    document.getElementById(component+idx).disabled = false; 
 
    } 
 

 
    // Get each component of the address from the place details 
 
    // and fill the corresponding field on the form. 
 
    for (var i = 0; i < place.address_components.length; i++) { 
 
    var addressType = place.address_components[i].types[0]; 
 
    if (componentForm[idx][addressType]) { 
 
     var val = place.address_components[i][componentForm[idx][addressType]]; 
 
     document.getElementById(addressType+idx).value = val; 
 
    } 
 
    } 
 
    //var keys=[];for (var key in place.address_components[0]) keys.push(key); 
 
    //alert(keys): 
 
    document.getElementById('autocomplete'+idx).value = 
 
    place.address_components[0]['long_name'] + ' ' + 
 
    place.address_components[1]['long_name']; 
 
    
 
    /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/ 
 
    document.getElementById('route'+idx).value = ''; 
 
} 
 

 
function fillInAddress2(idx) { 
 
    // Get the place details from the autocomplete object. 
 
    var place = auto2complete[idx].getPlace(); 
 

 
    for (var component in componentForm2[idx]) { 
 

 
     document.getElementById(component+idx).value = ''; 
 
     document.getElementById(component+idx).disabled = false; 
 
    } 
 

 
    // Get each component of the address from the place details 
 
    // and fill the corresponding field on the form. 
 
    for (var i = 0; i < place.address_components.length; i++) { 
 
     var addressType = place.address_components[i].types[0]; 
 
     if (componentForm2[idx][addressType+'2']) { 
 
     var val = place.address_components[i][componentForm2[idx][addressType + '2']]; 
 
     document.getElementById(addressType + '2'+idx).value = val; 
 
     } 
 
    } 
 
    document.getElementById('auto2complete'+idx).value = 
 
    place.address_components[0]['long_name'] + ' ' + 
 
    place.address_components[1]['long_name']; 
 
    
 
    /*document.getElementById('route2').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete2').value : '');*/ 
 
    document.getElementById('route2'+idx).value = ''; 
 
} 
 
    // [END region_fillform] 
 

 
// [START region_geolocation] 
 
// Bias the autocomplete object to the user's geographical location, 
 
// as supplied by the browser's 'navigator.geolocation' object. 
 
function geolocate() { 
 
    if (navigator.geolocation) { 
 
    navigator.geolocation.getCurrentPosition(function(position) { 
 
     var geolocation = new google.maps.LatLng(
 
     position.coords.latitude, position.coords.longitude); 
 
     var circle = new google.maps.Circle({ 
 
     center: geolocation, 
 
     radius: position.coords.accuracy 
 
     }); 
 
     autocomplete.setBounds(circle.getBounds()); 
 
    }); 
 
    } 
 
} 
 

 
function geolocate2() { 
 
    if (navigator.geolocation) { 
 
     navigator.geolocation.getCurrentPosition(function(position) { 
 
     var geolocation = new google.maps.LatLng(
 
      position.coords.latitude, position.coords.longitude); 
 
     var circle = new google.maps.Circle({ 
 
      center: geolocation, 
 
      radius: position.coords.accuracy 
 
     }); 
 
     autocomplete2.setBounds(circle.getBounds()); 
 
     }); 
 
    } 
 
    } 
 
    // [END region_geolocation] 
 
initialize(); 
 

 
document.querySelector('#chbSame').addEventListener('change', checkedAddr); 
 

 

 
function checkedAddr() { 
 
    if (document.getElementById('chbSame').checked) { 
 
    document.getElementById('route2').value = document.getElementById('route').value; 
 
    document.getElementById('locality2').value = document.getElementById('locality').value; 
 
    document.getElementById('administrative_area_level_12').value = document.getElementById('administrative_area_level_1').value; 
 
    document.getElementById('postal_code2').value = document.getElementById('postal_code').value; 
 
    
 
document.getElementById('auto2complete').value = document.getElementById('autocomplete').value; 
 
    } else { 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>

편집 : 도시, 주, 우편 번호가 자동으로 작성되도록 발췌문 업데이트되었습니다.

+0

답변이 업데이트되었습니다. 원하는지 확인하십시오. 또한 체크 박스는 여전히 작동하지 않지만 그 부분을 처리 할 수 ​​있다고 생각합니다. – Prachit

+0

여러 가지 방법이 있습니다. 'var addressType'을'var addressType = 'b'+ place.address_components [i] .types [0];'로 바꿀 수 있습니다. – Prachit

2

우리가 할 수있는 첫번째 일은 당신의 자바 스크립트를 정리하다 . 귀하의 페이지에 각 행에 대해 별도의 자바 스크립트가있는 것처럼 보입니다. 그럴 필요는 없습니다.

Cold Fusion과 마찬가지로 #form.static_and_#variable##을 말할 수는 없지만 배열 표기법을 사용할 수 있습니다 : #form["static_and_" & variable]#. 자바 스크립트에서는 object["static_and_" + variable]을 할 수 있습니다.

페이지에이 스크립트가 한 번만 필요하므로 루프 외부에 배치하십시오. 이제 n 변수를 전달합니다.

<script type="text/javascript"> 
    function FillBilling(f,n) { 
     if(f["billingtoo_" + n].checked == true) { 
      f["pstreet_" + n].value = f["street_" + n].value; 
      f["p2street_" + n].value = f["m2street_" + n].value; 
      f["pcity_" + n].value = f["city_" + n].value; 
      f["pstate_" + n].value = f["state_" + n].value; 
      f["ppostal_" + n].value = f["postal_" + n].value; 
     } 
    } 
</script> 

레이가 지적 하듯이, 당신은 확실히 getElementByID() 또는 (jQuery를 같은) 당신을 위해 (미안 넷스케이프 3, 당신은 운이있어) 합리적으로 이전 버전과의 호환성을 처리 심지어 라이브러리를 사용해야합니다.

<script type="text/javascript"> 
    function FillBilling(f,n) { 
     if(f.getElementByID("billingtoo_" + n).checked == true) { 
      f.getElementByID("pstreet_" + n.value = f.getElementByID("street_" + n).value; 
      // ... 
     } 
    } 
</script> 

그리고 확인란은 다음과 같은 기능을 호출합니다. cfoutput이 전체 태그를 래핑하는 방법에 유의하십시오. 이런 식으로하는 것이 실제로 해로울뿐만 아니라, 훨씬 더 읽기 쉽게 만들어줍니다. 그렇죠? 또한 일부 wysiwyg 편집기/코드 포맷터는 태그에 중첩 된 cf 태그를 사용하지 않습니다.

사실 모든 cfoutput을 사용하여 자바 스크립트를 읽는 데 어려움을 겪었습니다. ###으로 탈출 할 필요는 전혀 큰 문제가 아닙니다.

<cfoutput><input type="checkbox" name="billingtoo_#Add#" onclick="FillBilling(this.form,'#Add#')" id="billingtoo_#add#"></cfoutput> 

개인적으로 cfoutputs에서 가능한 한 많이 감 쌉니다. 나는 단지 체크 박스를 싸지 않을 것이다.

코드에서 사용하지 않는 ID의 이름이 같지만 요소가 동일한 ID를 공유합니다. 이로 인해 문제가 발생할 수 있습니다. DOM의 각 요소에는 기술적으로 자체 ID가 필요합니다 (충돌하는 경우 일반적으로 기능 할 수 있지만 ID로 참조하려는 경우에는 javascript로 인한 문제가 될 수 있음).

요소를 name 속성과 동일한 값으로 지정할 수 있습니다.이 값은 다소 편리합니다. 나는 두 개의 다른 이름을 기억할 필요가 없다. 그래서 당신은 할 수 있습니다 :

<input type="text" name="searchbox" id="searchbox" /> 
+2

RE :''몇 가지 : "id"값은 고유해야합니다. 또한 모든 단일 변수를 cfoutput에 래핑 할 필요가 없습니다. 이로 인해 코드가 읽기 편한 IMO가됩니다. 대신 전체 블록을 cfoutput에 래핑하십시오. 마지막으로, 매우 오래된/비표준 브라우저를 지원하지 않는 한 적절한 DOM 구문, 즉'document.getElementById()'를 사용하는 것이 좋습니다. 적절한 명명 규칙을 사용하면 FillBilling 메서드를 더욱 단순하게 만들 수 있습니다. @DavidBrierton - 자바 스크립트 오류 콘솔을보고 오류 메시지를 게시하십시오. – Leigh

+2

솔직히, 한 번에 너무 많은 일을하고있는 것처럼 보입니다. 위의 대답은 Google지도 기능이 아닌 FillBilling 기능 만 다루고 있습니다. 그 일을 먼저하십시오. 그런 다음 Google지도로 이동하십시오. 나는 그것에 대해 조사 할 시간이 없지만 문제의 적어도 일부는 DOM 요소 "id"의 *가 고유해야 함 *입니다. 그리고 코드가 고유 한 ID를 생성하지 않습니다. – Leigh

+0

@Leigh 체크 박스 참고 주셔서 감사합니다, 나는 확실히 그것을 커버하고 의미를 산만해진 후 잊어 버렸습니다. 나는 그 밖의 모든 것에 동의한다. 나는 시간이 부족했다. cfoutputs 전체가 흐트러지면 놓칠 수 있습니다. –

1

내가 질문 권리를 이해하고 있다면, 당신은 입력 필드 동적 입력을 취득하는 개수에 따라 채우기 위해 노력하고 있습니다.(가) 여기

$(document).ready(function() { 
    $('#go').on('click', function() { 
     populateInputs($('#repeatCount').val()); 
    }); 
}); 

과 : 당신이 그것을 채우려 할 때마다이 함수를 호출

function populateInputs(repeatCount) { 
    for (i = 1; i <= repeatCount; i++) { 
     var myHtml = '<div class="clearfix">' 
     . 
     . 
     . 
     $('body').append(myHtml); 
    } 
} 

보다 : 그렇게하기 위해서는, 당신은 자신의 기능으로 HTML을 채우는 코드를 이동해야합니다 fiddle : http://jsfiddle.net/gettgpmj/4/