$(document).ready(function(){
// This function get the search results from Solr server
$("#submit").click(function(){
var query=getquerystring() ; //get the query string entered by user
// get the JSON response from solr server
var newquery=query;
$.ajax({
url: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",
async:false,
dataType: 'json',
success: function(newresult){
var html="<table><tr>"
var count=0;
var alt="NoImage";
var size="3pt";
var id;
var flag=1; // Flag for error messages
border="1";
var n=newresult.response.numFound
var uid=new Array(n);
// If no search results
if(newresult.response.numFound==0)
{
var msg= "<hr /><font size="+size+" >We're sorry, we found no results for <b>"+document.getElementById("queryString").value+"</font><hr />";
}
else
{
/* var msg= "<hr /><font size="+size+" >Total Results Found <b> "+ result.response.numFound+"</b> for "+"<b>"+document.getElementById("queryString").value+"</b> keyword</font><hr /> ";*/
// Parse solr response and display it on web page
$.each(newresult.response.docs, function(i,item){
uid[i]=item.UID_PK;
});
$.each(newresult.highlighting, function(i,item1){
alert(uid[i]);
});
}
});
});
// get the query string entered by user
function getquerystring() {
var query=document.getElementById("queryString").value;
//qstr = escape(query);
return query;
}
});
在这个获取
值的uid []中$.each(newresult.response.docs, function(i,item){
uid[i]=item.UID_PK;
});
我想在下一个循环中访问此uid []
$。each(newresult.highlighting,function(i,item1){
alert(uid[i]);
});
但是在第2个each()循环中看不到uid [],此处的警报显示为undefined。为什么会这样?我需要做些什么才能使uid []在第二循环中可见。
答案 0 :(得分:0)
确保问题出在其他地方的一个测试:
1)你确定item.UID_PK包含什么吗?要确保这样做:
$.each(newresult.response.docs, function(i,item){
uid[i]=item.UID_PK;
alert(item.UID_PK);
});
此外,你在迭代什么?是一个对象吗?
编辑 - 看看你的json是什么问题,在你的第二个你在迭代一个对象,所以你的索引(i)是一个属性。 (在你的情况下,我等于8252和8142)所以你正在访问uid [8152]和uid [8142]。
我会做这样的事情:var uid;
$.each(newresult.response.docs, function(i,item){
uid=newresult.highlighting[item.UID_PK];
$.each(uid, function(l, desc){
alert(desc[0]);
});
});
提醒and <em>elegant</em> design was finely crafted in Japan.
和This <em>elegant</em> ring has an Akoya cultured pearl with a band of bezel-set round diamonds making