Jquery AutoComplete - not getting return values from functon

眉间皱痕 提交于 2019-12-23 02:44:09

问题


I'm sure there are a million things wrong with my coding...

However everything works EXCEPT - the autoComplete.

I can't seem to return values from my searchResults function.

I want to return the string.

Right now

  alert("t: " + availableTags);

works BUT

  alert("x: " + availableTags);

does not

Neither return lines return the values.

What am I doing wrong?

$(function(){
    //$("#searchButton").click(function(){
    //$("input").keyup(function(e){

    var availableCompanies = searchResults('CompanyName',false);
    var availableLocations = searchResults('Location',false);
    $("input[id^='CompanyName']").keyup(function(){ 
        //alert("CN");  
        searchResults('CompanyName',true);          
    });//$("input[id=CompanyName]").keyup(function(e){  
    $("input[id^='Location']").keyup(function(){    
        searchResults('Location',true);         
    }); 
    $("input[id^='serving']").keyup(function(){ 
        searchResults('serving',true);          
    });
    $( "#CompanyName" ).autocomplete({
        source: availableCompanies
    });
    $( "#Location" ).autocomplete({
        source: availableLocations
    });
    alert(availableCompanies);

})//$(function(){})  


function searchResults(which,populate,availableTags){
    //clear search results
    $("#searchResults").text("");
    //clear available tags
    //jqxAlert.alert(which);
    //var sSearch = $("#search").val();
    var sSearch =  $("input[id='"+which+"']").val();
    var url = "search.asp?" + which + "=" + sSearch;
    //alert(url);
    var availableTags = "";

    $.get(url, function(data){
        //$('#searchResults').html(data);
        var uid =""
        var company = ""; 
        var location = "";
        var phone1 = "";
        var phone2 = "";
        var phone3 = "";
        var html = "";
        var sql = "";
        var i = 1;


        if ($('company',data).text() == ""){
            $("#searchResults").html("<tr><td>Sorry, there are no results.</td></tr>");
        };

        $.each($('company',data),function(index, el) {  
            uid = $(this).find('uid').text();
            companyName = $(this).find('CompanyName').text();
            location = $(this).find('location').text();
            //phone1 = $(this).find('phone1').text();
            //phone2 = $(this).find('phone2').text();
            //phone3 = $(this).find('phone3').text();
            //serving = $(this).find('serving').text();
            //sql = "<br />" + $(this).find('sql').text() + "<br />";

            //alert(which == "CompanyName");
            if (which == "CompanyName"){
                //availableTags = availableTags + companyName;
                if (availableTags.indexOf(companyName) < 0){
                    if (availableTags == "") {
                        availableTags = companyName;
                    }else{
                        availableTags = availableTags + "," + companyName;
                    }
                }               
            }
            if (which == "Location"){
                if (availableTags.indexOf(Location) < 0){
                    if (availableTags == "") {
                        availableTags = Location;
                    }else{
                        availableTags = availableTags + "," + Location;
                    }
                }
            }   

            if (sSearch != ""){
                if (i % 2 == 0){
                    html = '<div id="c_' + uid + '" class="bgGrey">';
                }else{
                    html = '<div id="c_' + uid + '" class="bgWhite">';
                }
                html = html + "<tr>";
                html = html + "<td><b>&larr;</b>" + companyName + " (" + location + ")</td>";

                //html = html + company + " " + location ;
                html = html + "</tr>";
                html = html + "</div>";

                //$("#searchResults").append(i + ' ' +x + ' ' + y + ' ' + sql + html);
                $("#searchResults").append(html);
            }
            i++;
        }); //$.each($('company',data),function(index, el) {


        if (populate){
            PopulateFields();       
        }else{
            alert("t: " + availableTags);
            return "test";  
        }

    }).error(function() {
        $("#searchResults").html("<tr><td>Results could not be retrieved. Alert T. McDermott</td></tr>");
    }); //$.get(url, function(data){    
    alert("x: " + availableTags);
    return availableTags;   
}

回答1:


You're returning the value outside the ajax request, so the portion in $.get() is still getting data from the server. You can't operate like this since it's an asynchronous operation. You have 2 main options:

  1. Make it synchronous (bad, this locks up most browsers)
  2. Play nice with async behavior, by calling the next function when done.

What you should be doing is calling someOtherFunction(availableTags); at the end of your $.get(url, function(data){ }) function, where it has the data and can pass it along to whatever needs it.

What you're looking at is that the function(data) { } executes once the server has sent the data back to the browser and you can then process it, so you want to proceed from there.



来源:https://stackoverflow.com/questions/13997501/jquery-autocomplete-not-getting-return-values-from-functon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!