我确信我的编码有一百万个错误......
但是一切都有效 - 除了autoComplete。
我似乎无法从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>←</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;
}
答案 0 :(得分:1)
您在ajax请求之外返回值 ,因此$.get()
中的部分仍然从服务器获取数据。你不能这样操作,因为它是一个异步操作。您有两个主要选择:
您应该做的是在someOtherFunction(availableTags);
函数的末尾调用$.get(url, function(data){ })
,其中包含数据并可以将其传递给任何需要的函数。
您所看到的是,function(data) { }
在服务器将数据发送回浏览器后执行,然后您可以对其进行处理,因此您希望从那里开始。