如何使用Hubspot格式的邮政编码进行城市和州自动填充

时间:2014-06-03 16:54:04

标签: javascript forms

我试图弄清楚如何根据邮政编码输入制作我的Hubspot表单自动填充的城市和州字段。与此类似:https://zipcodedistanceapi.redline13.com/Examples

这是我目前的表格......

http://online.saintleo.edu/Request-Info-Splash.html

非常感谢有关如何实现这一目标的具体说明。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

下面是一段代码,请求发表第一条非常重要的评论:

<script type="text/javascript">
    $(function() {
        // IMPORTANT: Fill in your client key
        // IMPORTANT: Fill in your client key
        // IMPORTANT: Fill in your client key
        // IMPORTANT: Fill in your client key
        var clientKey = "js-9qZHzu2Flc59Eq5rx10JdKERovBlJp3TQ3ApyC4TOa3tA8U7aVRnFwf41RpLgtE7";// IMPORTANT: Fill in your client key
        // IMPORTANT: Fill in your client key
        // IMPORTANT: Fill in your client key
        // IMPORTANT: Fill in your client key
        // IMPORTANT: Fill in your client key


        var field = $("#zip");
        var errorDiv = container.find("div.text-error");
            var cache = {};

        function handleResp(data)
        {
            // Check for error
            if (data.error_msg)
                alert(data.error_msg);
            else if ("city" in data)
            {
                // Set city and state
                container.find("#city").val(data.city);
                container.find("#state").val(data.state);
            }
        }

        // Set up event handlers
        field.on("keyup change", function() {
            // Get zip code
            var zipcode = $(this).val().substring(0, 5);
            if (zipcode.length == 5 && /^[0-9]+$/.test(zipcode))
            {    
                // Check cache
                if (zipcode in cache)
                {
                    handleResp(cache[zipcode]);
                }
                else
                {
                    // Build url
                    var url = "https://zipcodedistanceapi.redline13.com/rest/"+clientKey+"/info.json/" + zipcode + "/radians";

                    // Make AJAX request
                    $.ajax({
                        "url": url,
                        "dataType": "json"
                    }).done(function(data) {
                        handleResp(data);

                        // Store in cache
                        cache[zipcode] = data;
                    }).fail(function(data) {
                        if (data.responseText && (json = $.parseJSON(data.responseText)))
                        {
                            // Store in cache
                            cache[zipcode] = json;

                            // Check for error
                            if (json.error_msg)
                                alert(json.error_msg);
                        }
                        else
                            alert('Request failed.');
                    });
                }
            }
        });
    });
</script>

答案 1 :(得分:0)

找到了一个很好的资源.......正是我所需要的。希望它能帮助别人!

http://code.google.com/p/ziplookup/