我继承了在SquirrelCart购物车应用程序中实现的SmartyStreets / LiveAddress代码,并且需要帮助我们弄清楚语法,以通过onclick事件将SmartyStreets / LiveAddress验证禁用,该事件会将帐单邮寄地址字段复制到送货地址字段中。

特别是对于国际地址(例如,在美国境外),在他们验证了帐单地址之后,必须对送货地址进行验证过程确实很烦人。

我目前已将地址初始化为:

/* SmartyStreets */
jQuery.LiveAddress(
    {
        key: "18924899",
        invalidMessage: "Not a valid US Postal address.</br>Please correct or certify below.",
        debug: "true",
        addresses: [
            // Squirrelcart billing address
            {
                id:         'Billing',
                street:     '#Bill_Street',
                city:       '#Bill_City',
                state:      '#Bill_State_or_Province',
                zipcode:    '#Bill_Postal_Code',
                country:    '#Bill_Country'
            },
            // Squirrelcart shipping address
            {
                id:         'Shipping',
                street:     '#Ship_Street',
                city:       '#Ship_City',
                state:      '#Ship_State_or_Province',
                zipcode:    '#Ship_Postal_Code',
                country:    '#Ship_Country'
            },
            // Squirrelcart account address
            {
                id:         'Account',
                street:     '#Street',
                city:       '#City',
                state:      '#State_or_Province',
                zipcode:    '#Postal_Code',
                country:    '#Country'
            }
        ],
        autoVerify: false,
        deactivate: 'Shipping'
    }
);


与将帐单地址复制到送货地址相关的HTML代码是:

<p>If your shipping address is the same as your billing address, clicking the
   "Same as Billing button will copy it into the fields below for you.
   If your shipping address is different, please type it in the fields below.</p>

<div align="center"><a class="btn btn_same_as_billing" href="#">Same as Billing</a></div>


相关的SquirrelCart JavaScript函数:

/*************************************************************
    Setup various aspects of the store
*************************************************************/
function scStoreSetup() {

    < ... snip ... >

    $$('.btn_same_as_billing').addEvent('click',function(evt) {scAddressCopy(evt);});

    < ... snip ... >

}

/*************************************************************
    Function copies fields from billing to shipping address
*************************************************************/
function scAddressCopy(evt) {
    // prevents the page from jumping position
    evt.stop();

    var form    = document.address_form;
    var billFldId, shipFldId, billFld, shipFld;

    // loop thru fields
    for(x=0; x < form.elements.length; x++) {
        billFldId   = form.elements[x].id;
        billFld     = form.elements[x];

        // if we are on a bill field
        if (billFldId.indexOf('Bill_') != -1) {
            // change id to matching ship field
            shipFldId = billFldId.replace('Bill_','Ship_');

            // grab ship field and set it
            shipFld = document.getElementById(shipFldId);
            if (shipFld) shipFld.value = billFld.value;
        }
    }
}

/*************************************************************
    Handle things specific to address form
*************************************************************/
function addrForm(elem, evt) {
    if (elem.id == 'Bill_State_Other' && evt.type == 'keyup') {
        if (elem.value.length) {
            // set state or province field to 'Other'
            if (document.getElementById('Bill_State_or_Province')) document.getElementById('Bill_State_or_Province').value = 2
        }
    } else if (elem.id == 'Ship_State_Other' && evt.type == 'keyup') {
        if (elem.value.length) {
            // set state or province field to 'Other'
            if (document.getElementById('Ship_State_or_Province')) document.getElementById('Ship_State_or_Province').value = 2
        }
    } else if (elem.id == 'Bill_State_or_Province' && evt.type == 'change') {
        if (document.getElementById('Bill_State_Other')) {
            var stateOther = document.getElementById('Bill_State_Other');
            if (elem.options[elem.selectedIndex].value == '2') {
                stateOther.value = '';
                stateOther.focus();
            } else {
                stateOther.value='';
            }
        }
    } else if (elem.id == 'Ship_State_or_Province' && evt.type == 'change') {
        if (document.getElementById('Ship_State_Other')) {
            var stateOther = document.getElementById('Ship_State_Other');
            if (elem.options[elem.selectedIndex].value == '2') {
                stateOther.value = '';
                stateOther.focus();
            } else {
                stateOther.value='';
            }
        }
    }
}


似乎我应该能够通过使用onclick事件来禁用或停用LiveAddress(尤其是国际地址),例如,

<a class="btn btn_same_as_billing" href="#" onclick="$('form#address_form').deactivate('Shipping');">Same as Billing</a>


其中“ address_form”是表单的ID:

<form id="address_form" class="sc_form" accept-charset="utf-8" name="address_form" action="<?php print $Form_Action?>" method="post">


我很难拨入语法。在这里帮我吗?

最好,
马蒂

最佳答案

    <input type="button" name="Copy" value="Copy Billing Address" id="billingtoshipping_checkbox">

    <script type="text/javascript">

    $(document).ready(function(){
        $("#billingtoshipping_checkbox").click(function(){
            $("[id^='shipping_']").each(function(){
                data=$(this).attr("id")
                tmpID = data.split('shipping_');
                $(this).val($("#billing_"+tmpID[1]).val())
            })
        });
    });

</script>

关于javascript - 如何通过onclick事件禁用SmartyStreets/LiveAddress,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27319230/

10-09 22:33