1

我敢肯定我的编码有一百万个问题......

然而,除了自动完成之外,一切正常。

我似乎无法从我的 searchResults 函数返回值。

我想返回字符串。

现在

  alert("t: " + availableTags);

工作但

  alert("x: " + availableTags);

才不是

返回行都不返回值。

我究竟做错了什么?

$(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;   
}
4

1 回答 1

1

您正在返回ajax request 之外的值,因此其中的部分$.get()仍在从服务器获取数据。你不能这样操作,因为它是一个异步操作。您有 2 个主要选项:

  1. 使其同步(不好,这会锁定大多数浏览器)
  2. 通过在完成后调用下一个函数来很好地处理异步行为。

你应该做的是someOtherFunction(availableTags);在你的函数结束时调用$.get(url, function(data){ }),它有数据并且可以将它传递给任何需要它的地方。

您正在查看的是function(data) { }服务器将数据发送回浏览器后执行,然后您可以处理它,因此您希望从那里继续。

于 2012-12-21T21:39:50.697 回答