need to add a word to a div if a certain object value is true
Okay, so I'm parsing some data from a webservice here. I'm just throwing in a bunch of values from a json object into html, however! i need to check if a certain json value is set to true or false. If so, i need to remove/add a word.
So i basicly i need to get a bunch of prices out. That's easy
$.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>
});
Now! i have to check for this value called FromPrice in the JSON object. And if that is true, then the word 'fra' should be added to the beginning of the the p tag with the class servicePrice.
Now, i certainly can't do it like this.
$.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>
});
That's just gonna add the word a whole bunch of times depending on how many loops we go through.
i've tried doing it the whole if thin开发者_JAVA技巧g inside the servicePrice tag. But that just gave me a whole lot of javascript parse errors.
Anyone wanna throw this guy a bone?
Update: I'm still not sure about this, but is this what you're looking for?
$.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>');
});
If I understood correctly then this should work.
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>
});
精彩评论