开发者

Problem in getting right result for select box

开发者 https://www.devze.com 2023-03-19 18:32 出处:网络
I am using jQuery as: $(document).ready(function(){ test("price"); alert("hi"); $("#item2").change(function()

I am using jQuery as:

$(document).ready(function(){
 test("price");
 alert("hi");
 $("#item2").change(function() 
    { 
        sort= $("#item2").val(); 
        test(sort);
    }); 
 });

Function test() is some JavaScript function, my problem is when page loads function calls by "price" parameter. Now when I select some item from select box function test() is called using sort parameter (verify by alert box). but I am not getting the correct result. I mean when I select option from select box than also my result of test() is as with "price" , I suppose it might be the problem because of jQuery's $(document).ready(function(){,. test() function make som开发者_Python百科e html code based on the parameter and show it on the web page. Please suggest me what can be the solution

EDIT:

function test() is :

function test(sort)
{
        <%
         Ampliflex ms = Ampliflex.getInstance();
         String solrIP = ms.getSolrIP();
         String solrPort = ms.getSolrPort();
         String rows = ms.getSearchResultCount();
        %>
        solrIP='<%= solrIP %>';     // get Solr IP address
        solrPort='<%= solrPort %>'; // get Solr Port number
        rows='<%= rows %>';         // get number of results to return
        solrURL="http://"+solrIP+":"+solrPort;
        var query="${searchStr}";   // get the query string entered by ECommerce user
        query=query.replace(/[^a-zA-Z 0-9*?:.+-^""_]+/g,''); // Remove special characters 
        query=query.replace(/\*+/g,'*'); // Replace multiple occurrence of "*" with single "*"
        var newquery=query;
        if(parseInt(query)==NaN)
        {
            var lowerCaseQuery=query.toLowerCase();
            newquery=lowerCaseQuery;
        }
        else{
        var lowerCaseQuery=query;
        }
        
        // sort= document.getElementById("item2").value;
        
        $.getJSON(solrURL+"/solr/db/select/?qt=dismax&wt=json&&start=0&rows="+rows+"&q="+lowerCaseQuery+"&hl=true&hl.fl=text&hl.usePhraseHighlighter=true&sort="+sort+" desc&json.wrf=?", function(result){
            var highlight = new Array(result.response.numFound);
            $.each(result.highlighting, function(i, hitem){
                        var rg = /<em>(.*?)<\/em>/g;
                        var res = new Array();
                        var match = rg.exec(hitem.text[0]);
                        while(match != null){
                                res.push(match[1])
                                match = rg.exec(hitem.text[0]);
                                }
                                highlight[i]=res[0]
                        for (j=1 ;j<res.length;j++)
                        {
                        highlight[i]= highlight[i]+","+res[j];
                        }
                    });
                
                    var html="<table><tr>"
                    var count=0;
                    var alt="NoImage";
                    var size="3pt";
                    var id;
                    var flag=1; // Flag for error messages 
                    border="1";
                    // If no search results
                    if(result.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 /> ";*/
                
                    if (newquery==lowerCaseQuery)
                    { 
                    var msg= "<hr /><font size="+size+" >Total Results Found <b>  "+ result.response.numFound+"</b> for "+"<b>"+query+"</b> </font><hr /> ";
                    }
                    else
                    {
                    var msg= "<hr /><font size="+size+" >There were no exact matches for <b>  "+ query+"</b> , so we searched automatically for  "+"<b>"+query+"</b> and yielded "+result.response.numFound+" result(s)</font><hr /> ";
                    }
                    // Parse solr response and display it on web page
                        $.each(result.response.docs, function(i,item){
                            var word = new Array();
                            word=highlight[item["UID_PK"]].split(",");
                            var result="";
                            var j=0;
                            for (j=0 ;j<=item.text.length;j++)
                            {
                                result = result+item.text[j]+"<br>";
                            }
                            for (j=0 ;j<word.length;j++)
                            {      
                                result=result.replace(word[j],'<em>' + word[j] + '</em>');
                            }
                            html+="<td><table>";
                            var src=item.image;
                            id="id";
                            if(src!= null && src!= ""){
                                    html+="<p><tr><td><br>"+"<img id= "+id+ " src="+src+ " border="+border+ "   /></td></tr>";
                                    count=count+1;
                                    
                                    html += "<tr><td><b>ImagePath</b>  "+ item.image+"</td></tr>";
                            }
                            // If not insert a default image  
                                  else
                                  {
                                 
                                      src="images/products/default.jpg";
                                      html+="<tr><td><br><p>"+"<img id= "+id+ " src="+src+ " border="+border+" /></td></tr>";
                                      count=count+1;
                                      html += "<tr><td><b>ImagePath</b>  "+"No image path found" +"</td></tr>";
                                  }
                                 html += "<tr><td>UID_PK: "+ item.UID_PK+"</td></tr>";
                                 html += "<tr><td>Name: "+ item.name+"</td></tr>";
                                 html+="<tr><td><b>Price: $"+item.price+"</td></tr>";
                                 html+="<tr><td> "+result+"<br></td></tr>";
                                 html+="</p></table></td>"
                            if(count%3==0)
                            {
                                html+="</tr>"
                                html+="<tr>"
                            }
                            
                    });
                    html+="</table>"
                }
                $("#text_container").html(msg);
                $("#result").append(html);
                
                
            }
        
    });
    
});
}


Your question isn't particularly clear, but your alert code only fires when the document is ready - it is not inside the "change" event function.

Try using the following to see what value is being returned when you change the select box:

$(document).ready(function(){
 test("price");
 $("#item2").change(function() 
    { 
        sort= $("#item2").val(); 
        alert(sort);
        test(sort);
    }); 
 });

When changing the select box, you should get an alert with the value you have chosen, which will help you understand why the test() function isn't functioning as you expect.

If you amend your question to include the HTML of the select box and the test() function itself I will amend my answer to help.


The JQuery code that you have posted is working fine. Demo: http://jsfiddle.net/DtnUr/

We need more details to figure out the issue, such as your HTML code and JS functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号