Javascript 按字段ID而不是按字段名复制表单元素

Javascript 按字段ID而不是按字段名复制表单元素,javascript,Javascript,我发现了一个名为copyShipping.js的文件,它允许我通过单击复选框将表单元素从一个表单复制到另一个表单。但是,它按每个字段的名称而不是ID进行复制。下面是代码。我怎样才能用身份证复印?我知道Javascript上有一个getElementByID,但我不知道如何实现它。理想情况下,我只希望为我更改代码。谢谢 function eCart_copyBillingToShipping(cb){ if(cb.checked){ // Only copy when the checkbox i

我发现了一个名为copyShipping.js的文件,它允许我通过单击复选框将表单元素从一个表单复制到另一个表单。但是,它按每个字段的名称而不是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;
                }
            }
        }
    }
}
}
功能eCart\u copyBillingToShipping(cb){
如果(cb.checked){//仅在选中复选框时复制。
var theForm=cb.form;
//billingFields和shippingFields中的元素数必须匹配。数组之间的输入元素类型也必须匹配。
var billingFields=新数组('firstname','lastname','email','phone','fax','street1','street2','city','StateOther','other_state_province','postcode','other_postcode','country');
var shippingFields=新数组('shipping_firstname','shipping_lastname','shipping_email','shipping_phone','shipping_fax','shipping_street1','shipping_street2','shipping_city','ShippingStateOther','其他_shipping_state_province','shipping_postcode','other_shipping_country');

对于(var i=0;i,建议始终使用Id访问元素。还建议使用相同的Id和名称。

试试这个

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;
                }
            }
        }
    }
}
}
功能eCart\u copyBillingToShipping(cb){
如果(cb.checked){//仅在选中复选框时复制。
var theForm=cb.form;
//billingFields和shippingFields中的元素数必须匹配。数组之间的输入元素类型也必须匹配。
var billingFields=新数组('firstname','lastname','email','phone','fax','street1','street2','city','StateOther','other_state_province','postcode','other_postcode','country');
var shippingFields=新数组('shipping_firstname','shipping_lastname','shipping_email','shipping_phone','shipping_fax','shipping_street1','shipping_street2','shipping_city','ShippingStateOther','其他_shipping_state_province','shipping_postcode','other_shipping_country');

对于(var i=0;i您是如何想到的?首先,按ID引用是不向后兼容的。其次,可能不可能对
name
ID
属性使用相同的值,因为a)ID必须是唯一的,控件名称不能,b)并非控件名称中允许的所有字符也可以在ID中使用。首先,在像IE7这样的浏览器中,通过ID分隔符引用,现在很少有人使用。名称和ID的值不要求相同,但开发人员尝试保持相同(当然,如果可能的话)正如良好的编码实践。它有助于可读性和可维护性。您说您不知道如何实现getElementById,所以我用它修改了代码。如果您不想使用它,请尝试使用eval('theForm.+shippingFields[I]),我想知道您的信息来自何处。1.提供了有效的标记,
document.getElementById(…)
在IE7中没有中断。它从IE/MSHTML5.0.2开始就受到支持。
eval()
是邪恶的[tm]。3.按普通属性名引用是专有的,不推荐使用,因为它容易出错。按名称引用表单控件的安全且符合标准的方法是使用集合:
theForm.elements[shippingFields[i]]
。您的目标浏览器是什么?在10年以下的浏览器版本中,您不必根据更改任何内容,并且集合可以使用名称和ID。此外,对于单选按钮和复选框组,按ID复制单个控件也没有意义。