【发布时间】:2011-11-07 22:00:24
【问题描述】:
如何在数组中搜索以查看值是否存在?
var fruitVarietyChecked = $('input[name=fruitVariety]:checked').val();
$.getJSON('getdata.php', {fruitVariety: fruitVarietyChecked}, function(fruittype) {
var html = '';
$.each(fruittype, function(index, array) {
alert( "Key: " + index + ", Value: " + array['fruittype'] );
//shows array - Key: 0 , Value: special item
//this is where the problem is
if ($(array.has("special item"))){
$("p").text("special item" + " found at " + index);
return false;
}
html = html + '<label><input type="radio" name="fruitType" value="' + array['fruittype'] + '" />' + array['fruittype'] + '</label> ';
});
$('#fruittype').html(html);
});
}
到目前为止,我尝试了 .is、.has、.getdata 和 .inarray,但它让我无处可去。
JSON 调用返回:[{"fruittype":"special item"},{"fruittype":"blue"},{"fruittype":"red"}]
【问题讨论】:
-
你的阵列是什么样子的?