jQuery ajax问题 - 无法让我的函数工作
16lz
2021-01-22
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();
}
更多相关文章
- 通过调用返回参数的本地函数来构建Ajax Data部分
- Jquery ajax回调函数不执行
- JQUERY组装对象并调用自身函数改变自己的属性
- Angular ng-show不会根据函数返回值显示/隐藏
- 在Paragraph标记中添加“名称”以使用POST
- 未捕获的ReferenceError:函数未定义,它标记
- 我怎么能用javascript编写这个简短的函数?
- 你能告诉为什么javascript函数没有在profile.php的提交按钮中执
- JQuery函数不能用于初始触发器