我还在学习JSON和JS,我知道这不是正确的方法。我有一堆循环,因为我需要在我的无序列表中显示特定项目。我希望有人可以帮助我,使这些代码更好地格式化,我知道必须有一个更好的方法来做到这一点。您将看到我按其ID定位每个项目并在UL中显示该项目。
$(document).ready(function() {
'use strict';
$.ajax({
dataType: "jsonp",
url: '',
success: function(data){
var recipeIngredients = "<ul class='recIngLink'>";
recipeIngredients += "<li>" + '2 tbsp butter' + "</li>";
$.each(data, function(i, item) {
if (item._id == "57911b238d30bc8e78002cf2") {
recipeIngredients += "<li>" + '3 tbsp ' + item.itemName + "</li>";
}
});
recipeIngredients += "<li>" + '1 onion, finely chopped' + "</li>";
$.each(data, function(i, item) {
if (item._id == "5846df5e06552ba30a00000d") {
recipeIngredients += "<li>" + '2 cloves ' + item.itemName + ', crushed' + "</li>";
}
});
$.each(data, function(i, item) {
if (item._id == "585c1f57fd50cb0000000257") {
recipeIngredients += "<li>" + '1 ½ cups ' + item.itemName + "</li>";
}
});
recipeIngredients += "<li>" + '1 cup pumpkin, diced' + "</li>";
recipeIngredients += "<li>" + '½ cup chopped parsley, optional' + "</li>";
recipeIngredients += "</ul>";
$('#recipeIngredients').append(recipeIngredients);
}
});
});