이 FIDDLE에서 봐 주시기 바랍니다. B 항목이 문자열 allergyfree
에 "글루텐"값이 없으면 -1
을 반환하는 이유는 무엇입니까?
가 여기에 JSON 파일이다 :
[{"title":"A","allergyfree":"sugar,salt,yeast,wheat,gluten"},{"title":"B","allergyfree":"sugar,salt,starch,yeast,wheat,preservatives"}]
스크립트 :
나는 값이 this thread에 존재하고 사람이inArray
기능을 사용하여 제안 여부를 확인하는 문제가 있기 때문에 나는이 질문을
$.ajax({
url: "url.json",
success: function (data) {
$(data.query.results.json.json).each(function (index, item) {
var title = item.title;
var allergyfree = item.allergyfree;
if(allergyfree.length !== 0) {
allergyfree = allergyfree.split(",");
$.each(allergyfree,function(i,allergy){
$('.'+title+allergy).append(jQuery.inArray(allergy, allergyfree));
});
}
});
},
error: function() {}
});
, 하지만 작동하지 않습니다.
@WesleyMurch,이 값이 존재하지 않을 때 정말 숫자를 반환 여부를 확인하기 위해 단지 데모입니다. 이 문'if ($ .inArray (allergyfree)! = - 1)'이 예상대로 작동하지 않는 이유를 찾아야합니다. – RedGiant