2014-12-07 154 views
0

我正在寫一個小代碼來解析jQuery中的XML,但只有一個字符正在從info屬性中讀取。XML解析 - jQuery

XML:

<series srss="29"> 
<srs info="2300,Sheffield Shield 2014-15"/> 
<srs info="2313,Ram Slam T20 Challenge 2014-15"/> 
<srs info="2275,Pakistan vs New Zealand in UAE, 2014"/> 
<srs info="2293,England tour of Sri Lanka, 2014"/> 
<srs info="2310,Ranji Trophy, 2014-15"/> 
<srs info="2278,India tour of Australia, 2014-15"/> 
<srs info="2262,West Indies in South Africa, 2014-15"/> 
<srs info="2302,Big Bash League 2014-15"/> 
<srs info="2291,Sri Lanka tour of New Zealand, 2014-15"/> 
<srs info="2279,India and England in Australia Tri-Series, 2015"/> 
<srs info="2292,Pakistan tour of New Zealand, 2015"/> 
<srs info="2325,WC Warm up matches, 2015"/> 
<srs info="2223,WC 2015"/> 
<srs info="2307,Irani Cup, 2015"/> 
<srs info="2321,County Championship Division One 2015"/> 
<srs info="2322,County Championship Division Two 2015"/> 
<srs info="2320,England tour of West Indies, 2015"/> 
<srs info="2323,NatWest t20 Blast, 2015"/> 
<srs info="2284,New Zealand tour of England, 2015"/> 
<srs info="2280,India tour of Bangladesh, 2015"/> 
<srs info="2285,The Ashes, 2015"/> 
<srs info="2324,Royal London One-Day Cup, 2015"/> 
<srs info="2286,Australia tour of Ireland, 2015"/> 
<srs info="2287,Australia tour of England, 2015"/> 
<srs info="2281,T20 WC League, 2015"/> 
<srs info="2282,South Africa tour of India, 2015"/> 
<srs info="2283,Sri Lanka tour of India, 2015-16"/> 
<srs info="2317,England tour of South Africa, 2015-16"/> 
<srs info="2263,T20 WC 2016"/> 
</series> 

代碼:

   var i=0; 
      seriesArray = $(text).find('srs').attr('info'); //Returns 2300,Sheffield Shield 2014-15 
      //console.log(seriesArray); 
      var SeriesVal = $(text).find('srs').each(function(){ 
       //console.log(this); 
       seriesArray[i] = $(this).attr('info'); //Returns 2 in first iteration, 3 in second iteration and so on 
       //console.log("Value = "+seriesArray[i]); 
       i=i+1; 
      }); 

一直堅持了這一點,可能是東西真的太小。有人可以幫忙嗎? 在此先感謝

回答

1

您沒有聲明seriesArray作爲數組。它只是設置爲第一個字符串,然後你訪問該字符串的部分,而不是創建一個數組。相反,使用

var seriesArray = []; 
seriesArray.push($(text).find('srs').attr('info')); //Returns 2300,Sheffield Shield 2014-15 

這裏有一個JSFiddle