0

XML ファイルから項目を選択する jquery を作成しようとしていますが、それらを正しく連鎖させることができないようです。

XML ファイルを解析して DOM に追加しました。これは構造の例です

<Products>
  <product1>
    <Description>Pound</Description>
    <ProductType>CUR</ProductType>
    <CurrencyCode>GBP</CurrencyCode>
    <Rate>1</Rate></ProductRate>
  </product1>
  <product2>
    <Description>Euro</Description>
    <ProductType>TCQ</ProductType>
    <CurrencyCode>Eur</CurrencyCode>
    <Rate>1.5</Rate></ProductRate>
  </product2>
</Products>

そこで私がやろうとしているのは、XML から取得して表示する要素を指定することです。XML からすべてのアイテムを選択できます。特定の要素を取得できますが、たとえばすべてを取得しようとすると(ProductType == "CUR")、コードとレートを選択してリストに追加できません。

var $xml = $(xml);
var $list = $('#list'); 
    $prodtype = $xml.find("ProductType");

    $prodtype.each(function() {
    if($(this).text() == "CUR"){ 
    $CurrencyCode = $xml.find("CurrencyCode");      
    $CurrencyCode.each(function() {
$( "#list" ).append("<li><a>" + $(this).text() + " </a></li>");
       });
    }
});

要素を選択して保存する方法を混乱させていると思います。したがって、擬似コードで私がやろうとしていることは

for each element where producttype == cur
grab next 2 siblings and append to list

これが明確であることを願っていますか?

4

4 に答える 4

1

これを試して -

$prodtype.each(function() {
    if($(this).text() == "CUR"){ 
    $CurrencyCode = $(this).siblings().find("CurrencyCode");
    $( "#list" ).append("<li><a>" + $CurrencyCode.text() + " </a></li>");        
    }
});

.siblings()の詳細について

于 2013-08-29T10:22:52.160 に答える
1

おそらく、parent() メソッドを使用してレベルを上げてから、子通貨 (および該当する場合はレート) に戻る必要があります。

$prodtype.each(function() {
    if($(this).text() == "CUR"){ 
      var $parent = $(this).parent();
      var $CurrencyCode = $parent.find("CurrencyCode");      
      $("#list").append("<li><a>" + $CurrencyCode.text() + " </a></li>");
    }
}
于 2013-08-29T10:14:42.017 に答える