首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过onclick事件禁用SmartyStreets/LiveAddress

如何通过onclick事件禁用SmartyStreets/LiveAddress
EN

Stack Overflow用户
提问于 2014-12-05 23:26:06
回答 1查看 576关注 0票数 1

我继承了在SquirrelCart购物车应用程序中实现的SmartyStreets/LiveAddress代码,需要帮助找出禁用SmartyStreets/LiveAddress验证的语法,并使用onclick事件将账单地址字段复制到发货地址字段中。

特别是对于国际地址(即美国以外的地址)在他们已经验证了帐单地址之后,必须通过发货地址的验证过程,这真的很烦人。

我目前的地址初始化如下:

代码语言:javascript
复制
/* 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代码为:

代码语言:javascript
复制
<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函数:

代码语言: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 (特别是对于国际地址),例如,

代码语言:javascript
复制
<a class="btn btn_same_as_billing" href="#" onclick="$('form#address_form').deactivate('Shipping');">Same as Billing</a>

其中"address_form“是表单的id:

代码语言:javascript
复制
<form id="address_form" class="sc_form" accept-charset="utf-8" name="address_form" action="<?php print $Form_Action?>" method="post">

我真的很难把语法拨进去。帮我一下?

最好的,马蒂

EN

回答 1

Stack Overflow用户

发布于 2017-11-20 20:35:46

代码语言:javascript
复制
    <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27319230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档