如何在两个不同的事件上在相同的HTML控件(文本字段)上调用两个不同的javascript / jquery函数?

时间:2014-09-15 10:52:16

标签: javascript jquery html function

以下是我为编写函数的textfield所拥有的HTML代码:

<input type="text" class="form-control" size="20" onblur="GetLocation();"  id="zip_code" name="zip_code" value="">

<input type="text" class="form-control" size="20" readonly="readonly"    id="store_longitude" name="store_longitude" value="">

<input type="text" class="form-control" size="20" readonly="readonly"    id="store_latitude" name="store_latitude" value="">

<input type="text" class="form-control"  name="state_code" id="state_code" value="" size="20">

<input type="text" class="form-control" maxlength="50" name="city" id="city" value="">

jQuery / Javascript代码如下:

$(document).ready(function() {
/*jQuery code for autopo-populate city and state when customer enters valid zip code*/
  $("#zip_code").keyup(function() {
    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) {
          $("#city").val(result.city);
          $("#state_code").val(result.state);
        }
      });
    }
  });
});

function GetLocation() {
    var geocoder = new google.maps.Geocoder();

    var saddress = $("#store_address").val();
    var store_state = $("#store_state").val();
    var store_country = $("#store_country").val();
    var store_zipcode = $("#store_zipcode").val();

    var complete_address = saddress +","+ store_state +","+ store_country +","+ store_zipcode;
    //alert(complete_address);
    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();
            //alert(longitude);
            $("#store_longitude").val(longitude);
            $("#store_latitude").val(latitude);
        } else {
            alert("Request failed.")
        }
    });
 };

如果邮政编码的文本字段失去焦点,即在填写此文本字段中的值(邮政编码)之后,我应该如何调用上述两个函数? 有人可以请你这方面指导我吗? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

好吧,让我们这样做:改变你的js

function GetLocation() {
    var geocoder = new google.maps.Geocoder();

    var saddress = $("#store_address").val();
    var store_state = $("#store_state").val();
    var store_country = $("#store_country").val();
    var store_zipcode = $("#store_zipcode").val();

    var complete_address = saddress +","+ store_state +","+ store_country +","+ store_zipcode;
    //alert(complete_address);
    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();
            //alert(longitude);
            $("#store_longitude").val(longitude);
            $("#store_latitude").val(latitude);
        } else {
            alert("Request failed.")
        }
    });
 $("#zip_code").keyup(function() {
    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) {
          $("#city").val(result.city);
          $("#state_code").val(result.state);
        }
      });
    }
  });
 }

现在在你的Focus OUT上:

$( "#zip_code" ).focusout(function() {
       GetLocation();
  })