变量是不可见的,它在一个循环中获取值,并在ajax中的另一个循环中访问

时间:2022-01-23 22:43:05
$(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;
     }
  });

in this uid[] which gets the value in

在这个获得价值的uid []中

$.each(newresult.response.docs, function(i,item){

                        uid[i]=item.UID_PK;
      });

i want to access this uid[] in next loop

我想在下一个循环中访问此uid []

$.each(newresult.highlighting, function(i,item1){

$ .each(newresult.highlighting,function(i,item1){

                alert(uid[i]);
            });

but uid[] is not visible in 2nd each() loop, the alert shows undefined here. why is so?? what i need to do to make uid[] visible in 2nd loop.

但是在第2个each()循环中看不到uid [],此处的警报显示未定义。为什么会这样?我需要做些什么才能使uid []在第二循环中可见。

1 个解决方案

#1


0  

One test to be sure that the problem lies somewhere else:

一个测试,以确保问题出在其他地方:

1)are you sure that item.UID_PK contains something?To be sure do this:

1)你确定item.UID_PK包含的东西吗?要确保这样做:

$.each(newresult.response.docs, function(i,item){

                        uid[i]=item.UID_PK;
                         alert(item.UID_PK);

      });

Moreover what are you iterating on?is item an Object?

而且你在迭代什么?是一个对象?

EDIT - looking at what your json is the problem is that in you second each you are iterating over an object and so your index (i) is a property. (in your case i is equal to 8252 and 8142) so you are accessing uid[8152] and uid[8142].

编辑 - 看看你的json是什么问题,在你的第二个你在迭代一个对象,所以你的索引(i)是一个属性。 (在你的情况下,我等于8252和8142)所以你正在访问uid [8152]和uid [8142]。

i'd do something like this:

我会做这样的事情:

var uid;
$.each(newresult.response.docs, function(i,item){
                  uid=newresult.highlighting[item.UID_PK];
                      $.each(uid, function(l, desc){
                              alert(desc[0]);
                       });

           });

alerts and <em>elegant</em> design was finely crafted in Japan. and This <em>elegant</em> ring has an Akoya cultured pearl with a band of bezel-set round diamonds making

警报和优雅的 设计在日本精心制作。这款优雅 戒指采用日本Akoya养珠设计,镶有一圈包边镶圆形钻石

look at the fiddle: http://jsfiddle.net/r6ZvT/

看看小提琴:http://jsfiddle.net/r6ZvT/

#1


0  

One test to be sure that the problem lies somewhere else:

一个测试,以确保问题出在其他地方:

1)are you sure that item.UID_PK contains something?To be sure do this:

1)你确定item.UID_PK包含的东西吗?要确保这样做:

$.each(newresult.response.docs, function(i,item){

                        uid[i]=item.UID_PK;
                         alert(item.UID_PK);

      });

Moreover what are you iterating on?is item an Object?

而且你在迭代什么?是一个对象?

EDIT - looking at what your json is the problem is that in you second each you are iterating over an object and so your index (i) is a property. (in your case i is equal to 8252 and 8142) so you are accessing uid[8152] and uid[8142].

编辑 - 看看你的json是什么问题,在你的第二个你在迭代一个对象,所以你的索引(i)是一个属性。 (在你的情况下,我等于8252和8142)所以你正在访问uid [8152]和uid [8142]。

i'd do something like this:

我会做这样的事情:

var uid;
$.each(newresult.response.docs, function(i,item){
                  uid=newresult.highlighting[item.UID_PK];
                      $.each(uid, function(l, desc){
                              alert(desc[0]);
                       });

           });

alerts and <em>elegant</em> design was finely crafted in Japan. and This <em>elegant</em> ring has an Akoya cultured pearl with a band of bezel-set round diamonds making

警报和优雅的 设计在日本精心制作。这款优雅 戒指采用日本Akoya养珠设计,镶有一圈包边镶圆形钻石

look at the fiddle: http://jsfiddle.net/r6ZvT/

看看小提琴:http://jsfiddle.net/r6ZvT/