English 中文(简体)
Google地图 Places API V3 自动完成 - 在输入时选择第一个选项( 并保持原样) [重复]
原标题:Google maps Places API V3 autocomplete - select first option on enter (and have it stay that way) [duplicate]

这个问题涉及到对此问题的答案 : < a href=" https://stackoverflow.com/ questions/7866446/google- magles-places-api-v3-autcompet-select-f-first-option-on-ent" > Google 地图 Places API V3 自动完成 - 在输入 时选择第一个选项。 基本上, 是要让字段在用户按键进入时使用自动完整列表的第一个建议。 这个问题的答案有一个jsfidle - < a href=" http://jsfiddle. net/dodger/pbbhH/ < a > - 。, 除非文本字段失去焦点, 字段值返回部分输入值 。

例如,输入字段和类型 ox 中的用户单击,自动完整的框会弹出一些建议,用户按键会输入。然后对地图进行修改,以显示第一个项目的位置,从自动完整的框(带有标记)中显示第一个项目的位置,输入字段的价值则从自动完整的框中改为第一个项目。然后用户单击字段以外的某个地方,输入字段返回到 ox 。

我希望输入字段的价值作为第一个自动完成的建议留在原处。

最佳回答

尝试此选项 : < a href=" "http://jsfiddle.net/pbbhH/60/" rel=" noreferrer" >http://jsfiddle.net/pbbhH/60/

基本上将选择逻辑抽象为新函数 < 坚固> 选择FirstResult () 。 然后将此函数调用为按下输入和丢失对文本的焦点 。

 function selectFirstResult() {
    infowindow.close();
    var firstResult = $(".pac-container .pac-item:first").text();

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({"address":firstResult }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var lat = results[0].geometry.location.lat(),
                lng = results[0].geometry.location.lng(),
                placeName = results[0].address_components[0].long_name,
                latlng = new google.maps.LatLng(lat, lng);

            moveMarker(placeName, latlng);
            $("input").val(firstResult);
        }
    });   
 }

EDIT: 对以下的 @ben s 评论略作改动。

问题回答

This is right but if u press enter and you have already selected an item this will select the first. So use this code:

function selectFirstResult() {
infowindow.close();
if ( $( .pac-selected ).length < 0){ // this line

var firstResult = $(".pac-container .pac-item:first").text();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":firstResult }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var lat = results[0].geometry.location.lat(),
            lng = results[0].geometry.location.lng(),
            placeName = results[0].address_components[0].long_name,
            latlng = new google.maps.LatLng(lat, lng);

        moveMarker(placeName, latlng);
        $("input").val(firstResult);
    }
});
}   
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签