输入更改后删除内容

时间:2014-12-05 12:01:23

标签: javascript jquery html

这是我的代码: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;
        }
    }
}

使用google autosuggest我可以使用返回的信息填充字段。但是,当发生以下情况时,我遇到了问题:

用户在地址的一部分中输入 点击自动填充(填写字段) 用户删除或修改当前输入 Uuser在新输入中键入但未单击自动完成

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

有人可以告诉我这是怎么做到的吗?我的猜测是这样的:

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

1 个答案:

答案 0 :(得分:0)

您需要手动存储旧值。您可以使用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