如何将SmartyStreets验证应用于同一页面中的两个表单?

时间:2015-11-11 01:51:17

标签: javascript jquery smartystreets

``` .COL-MD-6 %h3美国地址

%form.form水平 %标签街道地址 %input.form-control {id:'street',name:'street',style:'margin-bottom:10px; font-size:13px; height:30px“'}

%标签城市 %input.form-control {id:'city',name:'city',style:'margin-bottom:10px; font-size:13px; height:30px“'}

%标签状态 %input.form-control {id:'state',name:'state',style:'margin-bottom:10px; font-size:13px; height:30px“'}

%标签邮政编码 %input.form-control {id:'zipcode',name:'zipcode',style:'margin-bottom:10px; font-size:13px; height:30px“'}

.row   %BR   %BR   %input.btn.btn-ss-alt.btn-lg {type:“submit”,name:“commit”,style:“margin-bottom:20px; float:right; margin-right:15px; padding:10px 72px ;“}

.COL-MD-6 %h3国际地址

%form.form水平 %标签街道地址 %input.form-control {id:'street',name:'street',style:'margin-bottom:10px; font-size:13px; height:30px“'}

%标签城市 %input.form-control {id:'city',name:'city',style:'margin-bottom:10px; font-size:13px; height:30px“'}

%标签状态 %input.form-control {id:'state',name:'state',style:'margin-bottom:10px; font-size:13px; height:30px“'}

%标签邮政编码 %input.form-control {id:'zipcode',name:'zipcode',style:'margin-bottom:10px; font-size:13px; height:30px“'}

.row   %BR   %BR   %input.btn.btn-ss-alt.btn-lg {type:“submit”,name:“commit”,style:“margin-bottom:20px; float:right; margin-right:15px; padding:10px 72px “;} ```

SmartyStreets插件仅适用于第一种形式,不适用于第二种形式。

var liveaddress = $.LiveAddress({ key: #{ENV['SMARTY_STREETS']}, debug: true, addresses: [{ street: '#street', city: '#city', state: '#state', zipcode: '#zipcode' }] });

1 个答案:

答案 0 :(得分:2)

简单,只需将每个表单包装在表单标记中,并为每个字段指定一个不同的名称(或id)。该插件将接收它。这是一个使用自定义字段映射的两个表单的示例:

http://jsfiddle.net/p02qxh0L/69/

以下是使用自动映射在同一页面上显示16个表单的示例:

https://smartystreets.com/docs/plugin/examples

示例HTML:

    <form id="shipping">
    <input type="text" id="pais" name="pais" placeholder="pais">
    <br>
    <br>
    <input type="text" id="calle" name="calle" placeholder="calle">
    <br>
    <input type="text" id="ciudad" name="ciudad" placeholder="ciudad">
    <br>
    <input type="text" id="estado" name="estado" placeholder="estado">
    <br>
    <input type="text" id="codigo" name="codigo" placeholder="codigo">
    <br>
</form>
<hr>
<form id="billing">
    <input type="text" id="pais2" name="pais2" placeholder="pais2">
    <br>
    <br>
    <input type="text" id="calle2" name="calle2" placeholder="calle2">
    <br>
    <input type="text" id="ciudad2" name="ciudad2" placeholder="ciudad2">
    <br>
    <input type="text" id="estado2" name="estado2" placeholder="estado2">
    <br>
    <input type="text" id="codigo2" name="codigo2" placeholder="codigo2">
    <br>
</form>  

示例Javascript

    evar ss = jQuery.LiveAddress({
    key: '5640108848371823003',
    waitForStreet: true, 
    debug: true,
    addresses: [{
        country: '#pais',
        street: '#calle',
        city: '#ciudad',
        state: '#estado',
        zipcode: '#codigo'
    },{
        country: '#pais2',
        street: '#calle2',
        city: '#ciudad2',
        state: '#estado2',
        zipcode: '#codigo2'
    }]
});