2014-12-05 110 views
1

这是我的代码:http://jsfiddle.net/spadez/6v3kbLbk/7/删除内容时,输入切换

var placeSearch, autocomplete; 
var componentForm = { 
    route: 'long_name', 
    locality: 'long_name', 
    administrative_area_level_1: 'long_name', 
    country: 'long_name', 
    postal_code: 'short_name' 
}; 

function initialize() { 
    autocomplete = new google.maps.places.Autocomplete(
    (document.getElementById('autocomplete')), { 
     types: ['geocode'] 
    }); 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     fillInAddress(); 
    }); 
} 

function fillInAddress() { 
    var place = autocomplete.getPlace(); 

    for (var component in componentForm) { 
     document.getElementById(component).value = ''; 
     document.getElementById(component).disabled = false; 
    } 

    for (var i = 0; i < place.address_components.length; i++) { 
     var addressType = place.address_components[i].types[0]; 
     if (componentForm[addressType]) { 
      var val = place.address_components[i][componentForm[addressType]]; 
      document.getElementById(addressType).value = val; 
     } 
    } 
} 

使用谷歌自动提示我能够填充字段机智HTHE返回的信息。不过,我碰到一个问题,当出现以下情况:

用户键入一个地址 点击自动完成(其中费尔场) 用户删除或新的输入修改电流输入 Uuser类型,但犯规点击自动完成

的一部分

这会在自动填充字段中留下旧值。我需要的是检查字段中的内容是否已更改,如果有,请删除带有红色边框的字段中的内容。

任何人都可以告诉我这可能是怎么完成的?我的猜测是这样的:

$('#autocomplete').on('input', function() { 
    // do something }); 
+0

你可以考虑如果用户点击任何建议清空您的输入,然后把一个新的 – 2014-12-05 12:10:52

+0

但问题是关于如果用户点击自动提示的一个地址,但改变它,它不会更新自动完成字段 – Jimmy 2014-12-05 12:23:28

回答

0

你需要存储的旧值manually.You可以使用JavaScript对象来存储值对每个文本框:

function onChangeTest(textbox) { 
    alert("Value is " + textbox.value + "\n" + "Old Value is " + textbox.oldvalue); 
} 

<input class="input hidden" id="country" disabled="true" onfocus="this.oldvalue = this.value;" onchange="onChangeTest(this);this.oldvalue = this.value;"> 

Demo

+0

关于使用什么g“on”还是keyup? http://stackoverflow.com/questions/6458840/on-input-change-event – Jimmy 2014-12-05 13:10:01