I'm attempting to customize an autocomplete plugin for jquery. This should be extremely simple.

我正在尝试为jquery自定义一个自动完成插件。这应该非常简单。

It is supposed to pull the list of names from autocomplete.php and then send to "lookup". It works when I type it out manually, but when I try to get the names with the function getnames() it doesn't work. Am I returning the data correctly? The php file works as well.

它应该从autocomplete.php中提取名称列表,然后发送到“查找”。当我手动输入它时,它工作,但当我尝试使用函数getnames()获取名称时,它不起作用。我正确地返回数据了吗? php文件也可以。

var options, a;

function getnames() {
    $.ajax({
        type:'POST',
        dataType:'json',
        data:{ },
        url:'autocomplete.php',
        timeout:1000,
        success:function (data) {
            return data;
        }
    });
}

// Doesn't work:

// DOC READY
$("#members").one("click", function () {
    options = { 
        lookup:getnames() 
    };
    a = $('#members').autocomplete(options);
});

// Works:

// DOC READY
$("#members").one("click", function () {
    options = { 
        lookup:["name1","name2"]
    };
    a = $('#members').autocomplete(options);
});

autocomplete.php

<?
header("Content-Type: application/json", true);
$info = array("name1","name2");
echo json_encode($info);
?>

2 个解决方案

#1


1

You have get the names first and wait for the ajax response. When it arrives you have to start the autocomplete.

您先获取名称并等待ajax响应。当它到达时,你必须开始自动完成。

E.g. (untested):

function getnames() {
    $.ajax({
        type:'POST',
        dataType:'json',
        data:{ },
        url:'autocomplete.php',
        timeout:1000,
        success:function (data) {
            options = { 
                lookup:data
            };
            a = $('#members').autocomplete(options);
        }
    });
}

$("#members").one("click", function () {
    getnames();
}

更多相关文章

  1. 通过调用返回参数的本地函数来构建Ajax Data部分
  2. Jquery ajax回调函数不执行
  3. JQUERY组装对象并调用自身函数改变自己的属性
  4. Angular ng-show不会根据函数返回值显示/隐藏
  5. 在Paragraph标记中添加“名称”以使用POST
  6. 未捕获的ReferenceError:函数未定义,它标记
  7. 我怎么能用javascript编写这个简短的函数?
  8. 你能告诉为什么javascript函数没有在profile.php的提交按钮中执
  9. JQuery函数不能用于初始触发器

随机推荐

  1. Android软件的自动更新
  2. 欢迎进入Android的世界
  3. Android之我当年爬过的坑
  4. Android修改APP版本号
  5. Android(安卓)UI 之 我的页面 圆形图片+
  6. android最佳实践(五)
  7. Android单行显示ellipse和singleLine
  8. android基础入门(二)——创建android工程
  9. 如何安装apk文件在Android仿真器中
  10. Android探索之旅 | Android简介