2013-10-17 31 views
0

我正在試圖將「if」條件應用於AJAX xml解析,但我不能。這是我的代碼將condicional應用於AJAX xml解析

<script> 

$(document).ready(function() 
    { 
      $.ajax({ 
      type: "GET", 
      url: "http://domain.com/file.xml", 
      dataType: "xml", 
      success: function(xml) { 

       $(xml).find("something").each(function(){ 
         if (data[i]["somefield"] == 1) { 
           $("#output").append("HOLAAAA"); 
         } else { 
           $("#output").append("MOLOOOOOOOOOO"); 
         } 

        }); 
      }, 
      error: function() { 
       alert("Can't retrieve data"); 
      } 

      }); 

    }); 
</script> 

但是它不運行。有誰能夠幫助我?

非常感謝您

+0

什麼你試圖做的是不明確的,但你應該使用傳遞給你給每一個回調的參數:'$ (xml).find(「something」)。each(function(SOME ARGS){'。 –

+0

'i'聲明在哪裏??通過jquery'each'函數傳遞它.. –

+0

謝謝.LeGEC的答案解決了我的問題問題。乾杯。 – user2851282

回答

0

我想,你data[i]是您試圖訪問循環內的電流"something"節點。

內回調,您應該通過this訪問當前節點:

// if "somefield" is an attribute : 
    if ($(this).attr("somefield") == 1) { 
     $("#output").append("HOLAAAA"); 
    } else { 
     $("#output").append("MOLOOOOOOOOOO"); 
    } 

    // if "somefield" is a child node : 
    if ($(this).children("somefield").text() == 1) { 
     $("#output").append("HOLAAAA"); 
    } else { 
     $("#output").append("MOLOOOOOOOOOO"); 
    } 
+0

非常感謝,它解決了我的問題。 – user2851282