0

我发现了一个名为 copyShipping.js 的文件,它允许我通过单击复选框将表单元素从一个表单复制到另一个表单。但是,它按每个字段的名称而不是 ID 进行复制。下面是代码。我如何让它通过ID复制?我知道 Javascript 上有一个 getElementByID,但我不知道如何实现它。理想情况下,我只想为我更改代码。谢谢。

function eCart_copyBillingToShipping(cb){
if(cb.checked){ // Only copy when the checkbox is checked.
    var theForm = cb.form;
    // The number of elements must match in billingFields and shippingFields. The type of input element must also match between the arrays.
    var billingFields = new Array('firstname', 'lastname', 'email', 'phone', 'fax', 'street1', 'street2', 'city', 'state_province', 'StateOther', 'other_state_province', 'postcode', 'other_postcode', 'country');
    var shippingFields = new Array('shipping_firstname', 'shipping_lastname', 'shipping_email', 'shipping_phone', 'shipping_fax', 'shipping_street1', 'shipping_street2', 'shipping_city', 'shipping_state_province', 'ShippingStateOther', 'other_shipping_state_province', 'shipping_postcode', 'other_shipping_postcode', 'shipping_country');

    for(var i=0;i<billingFields.length;i++){
        var billingObj = theForm.elements[billingFields[i]];
        var shippingObj = theForm.elements[shippingFields[i]];
        if(billingObj && shippingObj){
            if(billingObj.tagName){ // non-radio groups
                var tagName = billingObj.tagName.toLowerCase();
                if(tagName == 'select'){
                    shippingObj.selectedIndex = billingObj.selectedIndex;
                }
                else if((billingObj.type && shippingObj.type ) && (billingObj.type == 'checkbox' || billingObj.type == 'radio')){
                    shippingObj.checked = billingObj.checked;
                }
                else{ // textareas and other inputs
                    shippingObj.value = billingObj.value;
                }                   
            }
            else if(billingObj.length){ // radio group
                for(var r=0;r<billingObj.length;r++){
                    shippingObj[r].checked = billingObj[r].checked;
                }
            }
        }
    }
}
}
4

1 回答 1

0

始终建议使用 Id 访问元素。此外,建议 id 和 name 相同。

尝试这个。

function eCart_copyBillingToShipping(cb){
if(cb.checked){ // Only copy when the checkbox is checked.
    var theForm = cb.form;
    // The number of elements must match in billingFields and shippingFields. The type of input element must also match between the arrays.
    var billingFields = new Array('firstname', 'lastname', 'email', 'phone', 'fax', 'street1', 'street2', 'city', 'state_province', 'StateOther', 'other_state_province', 'postcode', 'other_postcode', 'country');
    var shippingFields = new Array('shipping_firstname', 'shipping_lastname', 'shipping_email', 'shipping_phone', 'shipping_fax', 'shipping_street1', 'shipping_street2', 'shipping_city', 'shipping_state_province', 'ShippingStateOther', 'other_shipping_state_province', 'shipping_postcode', 'other_shipping_postcode', 'shipping_country');

    for(var i=0;i<billingFields.length;i++){
        //assuming that now array contains id (not name)
        var billingObj = theForm.getElementById(billingFields[i]);
        var shippingObj = theForm.getElementById(shippingFields[i]);
        if(billingObj && shippingObj){
            if(billingObj.tagName){ // non-radio groups
                var tagName = billingObj.tagName.toLowerCase();
                if(tagName == 'select'){
                    shippingObj.selectedIndex = billingObj.selectedIndex;
                }
                else if((billingObj.type && shippingObj.type ) && (billingObj.type == 'checkbox' || billingObj.type == 'radio')){
                    shippingObj.checked = billingObj.checked;
                }
                else{ // textareas and other inputs
                    shippingObj.value = billingObj.value;
                }                   
            }
            else if(billingObj.length){ // radio group
                for(var r=0;r<billingObj.length;r++){
                    shippingObj[r].checked = billingObj[r].checked;
                }
            }
        }
    }
}
}
于 2013-10-01T03:45:17.470 回答