2011-08-12 13 views
0

好的,所以我在這裏解析了一些來自web服務的數據。 但是,我只是將一堆json對象的值拋入html中!我需要檢查某個json值是否設置爲true或false。如果是這樣,我需要刪除/添加一個單詞。如果某個對象的值爲真,需要向某個div添加一個單詞

所以我基本上我需要得到一堆價格。這很容易

$.each(data.d, function (i, service) { 
      $(".services").append('<li class="classes here"><p class="servicePrice">'+service.Price+' kr</p><div class="serviceNames">' + service.Name +'<br />' + service.FirstAvailableSlot + '</div> 

}); 

現在!我必須在JSON對象中檢查這個名爲FromPrice的值。如果這是真的,那麼'fra'這個詞應該被添加到p標籤的開始,並且帶有類servicePrice。

現在,我當然不能這樣做。

$.each(data.d, function (i, service) { 
       if (service.PriceFrom == false) { $('.servicePrice').prepend('fra '); }; 
       $(".services").append('<li class="classes here"><p class="servicePrice">'+service.Price+' kr</p><div class="serviceNames">' + service.Name +'<br />' + service.FirstAvailableSlot + '</div> 

    }); 

這只是要添加一個詞的時間取決於我們經歷多少循環。

我試過這樣做,如果servicePrice標記內的東西。但是這只是給了我很多javascript解析錯誤。

任何人都想扔這個傢伙骨頭?

回答

0

更新:我對此仍不確定,但這是您要找的嗎?

$.each(data.d, function (i, service) { 
    var txt = (service.PriceFrom == false) ? "fra" : ""; 
    $(".services").append('<li class="classes here">'+txt+'<p class="servicePrice">'+service.Price+' kr</p><div class="serviceNames">' + service.Name +'<br />' + service.FirstAvailableSlot + '</div>'); 
}); 
+0

不幸的是,這似乎並沒有做到這一點,它增加了'fra',但與其應該沒有關係。如果我通過json對象查看當我調用webservice時得到的json對象,並用結果的「列表」對其進行交叉檢查。其中一些行不應該有'Fra'這個詞,但是他們中的每一個都有。有一個例外,最後一個例外。 – Niklas

+0

嘿,檢查我的更新。這是你在找什麼? – Mrchief

+0

正是我在找什麼!並且完全感到羞愧,我沒有想到自己。感謝@Mrchief的幫助! – Niklas

0

如果我理解正確,那麼這應該工作。

var isFalse = false; 
    $.each(data.d, function (i, service) { 
      if (service.PriceFrom == false && !isFalse) { 
       $('.servicePrice').prepend('fra '); 
       isFalse = true; 
      }; 
      $(".services").append('<li class="classes here"><p class="servicePrice">'+service.Price+' kr</p><div class="serviceNames">' + service.Name +'<br />' + service.FirstAvailableSlot + '</div> 

}); 
相關問題