一旦用户在输入字段中输入有效的美国邮政编码值,如何自动填充城市和州输入字段

How to auto-populate the city and state input fields once user enters the valid United States zip code value into an input field?

本文关键字:输入 字段 编码值 何自动 填充 城市 美国 用户 有效      更新时间:2023-09-26

实际上,我的网站中有这个工作功能。但突然间,我以AJAX响应的形式获取州和市的URL停止了工作。

现在我面临的最大问题是如何让这件事再次可行?

以下是我当前的代码:

HTML代码:

<input type="text" class="form-control" size="20"  id="store_zipcode" name="store_zipcode" value="">
    <input type="text" class="form-control" maxlength="50" readonly="readonly" name="store_city" id="store_city" value="">
    <input type="text" class="form-control"  readonly="readonly" name="store_state" id="store_state" value="" size="20">

jQuery AJAX代码:

//Function called in Store Add/Edit form when user enters valid US Zip Code value  
    function GetLocation() {
        var geocoder = new google.maps.Geocoder();      
        var store_zipcode = $("#store_zipcode").val();
        var complete_address = store_zipcode;
        geocoder.geocode({ 'address': complete_address }, function (results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            $("#store_longitude").val(longitude);
            $("#store_latitude").val(latitude);
          } else {
              //alert("Request failed.")
            }
        });
        var el = $(this);
        if (el.val().length === 5) {
          $.ajax({
            url: "http://zip.elevenbasetwo.com",
            cache: false,
            dataType: "json",
            type: "GET",
            data: "zip=" + el.val(),
            success: function(result, success) {
              $("#store_city").val(result.city);
              $("#store_state").val(result.state);
            }
          });
        }  
      }
      //Call to the function to display location details on Store add/edit form
      $( "#store_zipcode" ).focusout(GetLocation);

还有一件事是,我无法将城市和州的名称存储到我的数据库中。我只能像上面那样从一些数据源中获取数据。但数据来源应该是可靠的。功能永远不应该崩溃。

如果你想了解更多关于我所说问题的信息,你可以在以下URL查看我刚刚问的问题:链接到以前问的问题。

看来elevenbasetwo.com域当前没有联机。zip.evenbasetwo.com子域没有有效的DNS条目,因此目前不会产生任何结果。这可能只是暂时的事情——几个小时后再试一次。