2014-09-24 213 views
0

我试图将自动填充输入字段中的内容与formatted_address输出进行比较。地图API获取城市和国家

我限制我的自动完成自动完成与:

var options = { 
     types: ['(cities)'], 
    componentRestrictions: {country: "be"} 
    }; 

,然后采取输出城市的formatted_address。

一切工作正常,但对于一些城市我有一个数字,在输出中添加,我无法比较它。

实施例:
- 这是可比
输入字段所示=布鲁塞尔,比利时
Formatted_adress =布鲁塞尔,比利时

- 这是没有可比性
输入字段所示= Malonne,比利时
Formatted_adress = 5020 Malonne,Belgique

问题是:如何获得与显示输入字段完全相同的输出?

///////EDIT 

我以为我删除这些数字解决了这个问题:

//Delete numbers and spaces before output if they exist 
while(from.charAt(0) == " " || (from.charAt(0) >= '0' && from.charAt(0) <= '9')) 
    { 
    from = from.substr(1); 
    } 

但你也有不同的语言:
例子:
输入字段显示=安特卫普,België属于
输出=安特卫普,Belgique

+0

请发表您的解决方案作为一个答案,[接受](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) – geocodezip 2014-09-25 06:37:08

+0

我仍然没有解决问题。查看新的编辑 – gr3g 2014-09-25 14:21:18

回答

0

问题是将输入字段与隐藏的城市输入字段进行比较。
为什么?
1.Typing布鲁塞尔
2.选择布鲁塞尔
3.隐藏城市==布鲁塞尔,隐藏经纬度=(XX,XX)

如果提交 - >确定

如果键入布拉布拉也不要按回车键, blabla将会是前一个latlong隐藏输入的输入字段。

所以我想比较隐藏的城市和城市输入字段。

解决方法:
打字时 - >清除隐藏字段。

//Set from city hidden values to "" when typing again 
    var input1 = document.getElementById('connexion_inputcity'); 

    input1.onkeydown = function() { 
     var key = event.keyCode || event.charCode; 

     if(key == 8 || key == 46) 
      { 
      document.forms["inscription"]["check1"].value = ""; 
      document.forms["inscription"]["LatLongFrom"].value = ""; 
      } 
     }; 

    input1.onkeypress = function() { 
     document.forms["inscription"]["check1"].value = ""; 
     document.forms["inscription"]["LatLongFrom"].value = ""; 
    }; 


//Set to hidden hidden values to "" when typing again 
    var input2 = document.getElementById('connexion_inputNdCity'); 

    input2.onkeydown = function() { 
     var key = event.keyCode || event.charCode; 

     if(key == 8 || key == 46) 
      { 
      document.forms["inscription"]["check2"].value = ""; 
      document.forms["inscription"]["LatLongTo"].value = ""; 
      } 
     }; 

    input2.onkeypress = function() { 
     document.forms["inscription"]["check2"].value = ""; 
     document.forms["inscription"]["LatLongTo"].value = ""; 
    };