2013-10-17 88 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