2013-10-04 48 views
0

Ajax和XML解析器我在JS做錯了什麼?我需要一個座標到每個元素但不是所有

$.ajax({ 
    url: 'http://geocode-maps.yandex.ru/1.x/?geocode=43.2491,76.9198&sco=latlong&kind=house&results=7',//here get the XML wirh all adresses and coords 
    type: 'GET', 
    dataType: 'html', 
    success: function(d){ 
     $("#message").html(''); 
     var coord = $(d).find('pos').text(); 
     $(d).find('name').each(function(){ 
      $('#message').prepend("<div class='addresses' onClick='insert_adress($(this).text());'>"+$(this).text()+" и "+coord+"</div>"); //here it write to a box     
     }) 
    } 
}); 

here is example oj jssfidle

+2

什麼錯誤? – Misters

回答

0

請試試這個http://jsfiddle.net/cHJ6S/3/

... 
      $(d).find('GeoObject').each(function() { 
       var coord = $(this).find('pos').text(); 
       var name = $(this).find('name').text(); 
       $('#message').prepend("<div class='addresses' onClick='insert_adress($(this).text());'>"+name+" и "+coord+"</div>"); 
      }); 
... 

這是你wnat結果呢?

улица Муратбаева, 183 и 76.918695 43.249010 
улица Муратбаева, 185 и 76.919100 43.248557 
улица Муратбаева, 168 и 76.919692 43.248426 
улица Карасай батыра, 108 и 76.920294 43.249496 
улица Шагабутдинова, 103а и 76.920375 43.248991 
улица Муратбаева, 166 и 76.919989 43.248761 
улица Муратбаева, 164 и 76.919684 43.249214 
0

我可以建議你到XML轉換爲使用PHP JSON,然後將其發送給JS。

0

如果您已經檢查了控制檯,你會看到下面的錯誤

Uncaught TypeError: Object 76.919800 43.24910076.919684 43.24921476.919989 43.24876176.920375 43.24899176.920294 43.24949676.919692 43.24842676.919100 43.24855776.918695 43.249010 has no method 'next'

此行是你的問題的原因。

var coord = $(d).find('pos').text().next(); //remove next will fix it 

入住這JSFiddle

FYI:按F12,檢查控制檯有任何錯誤。

0

代替$(d).find('name'),

使用$(d).find('featuremember').each(function(){...}),

featuremember - 代表對象,那麼爲什麼通過對象不循環

然後內環

var pos = $($(this).find('pos')).text(); 
var name = $($(this).find('name')).text(); 

終於

$('someId').prepend(name + ' pos: ' + pos); 
使用
0

試試這個,

var coord = $(d).find('pos').text(); 

的完整代碼

$.ajax({ 
    url: 'http://geocode-maps.yandex.ru/1.x/?geocode=43.2491,76.9198&sco=latlong&kind=house&results=7', 
    type: 'GET', 
    dataType: 'html', 
    success: function(d){ 
     $("#message").html(''); 
     var coord = $(d).find('pos').text();// pos text not its next element's text 
     $(d).find('name').each(function(){ 
      $('#message').prepend("<div class='addresses' onClick='insert_adress($(this).text());'>"+$(this).text()+" и "+coord+"</div>");     
     }); 
    } 
}); 

Fiddle

相關問題