jQuery的自动完成功能使用JSON初学者问题 [英] Jquery Autocomplete with JSON beginners question
问题描述
我使用的是官方的jQuery的自动完成插件。我有广告商列表的ODBC数据库。我现在有一个输入框,其中名称的一部分的用户类型,我成功地返回部分匹配的列表。我遇到的问题是,我不知道怎么回的名称和广告客户的ID。我假设我会想用一个jQuery功能调整的隐藏字段的值到ID存储检索后。
I am using the official Jquery Autocomplete plugin. I have an ODBC database with a list of advertisers. I currently have an input box where the user types in part of the name and I am successfully returning a list of partial matches. The problem I'm having is I do not understand how to return both the name and the ID of the advertiser. I am assuming I will want to use a Jquery function to adjust the value of a hidden field to store the ID after retrieving it.
php文件返回需要进行修改,以返回UsageNumber以及广告客户名称:
php file to return the advertiser name that needs to be modified to return the UsageNumber as well:
$sql = "SELECT DISTINCT tNAdvertisors.UserName, tNAdvertisors.UsageNumber
FROM tNAdvertisors
WHERE (((tNAdvertisors.UserName) Like '$term%'));";
$rs=odbc_exec($conn,$sql);
$a = array();
while (odbc_fetch_row($rs)) {
$a[] = htmlentities(odbc_result($rs,"UserName"));
}
echo json_encode($a);
目前jQuery的code为需要进行修改,包括函数来处理ID自动完成?
current jquery code for the autocomplete that needs to be modified to include a function to handle the ID?
$("#single").autocomplete({
source: "ajaxSearchForAdvertiser.php",
minLength: 3
});
感谢您的时间和帮助。
推荐答案
从看的自动完成演示页面图片搜索,你可以试试重要的信息组合成一个字符串,比如从数据库中的广告客户名称ID(抱歉,我不能与帮助,因为我不知道PHP和放大器; SQL不够好):
From looking at the autocomplete demo page image search, you could try combining the important information into a string, say the ID with the advertiser's name from the database (sorry I can't help with that since I don't know php & SQL well enough):
1234#Exxon
2345#BP
3456#Shell
然后自动完成脚本将是这样的:
Then the autocomplete script would be something like this:
$("#imageSearch").autocomplete("images.php", {
formatItem: function(data, i, n, value) {
return value.split("#")[1]; // returns just the name for display in the input box
},
formatResult: function(data, value) {
var result = value.split('#');
return [ result[0], result[1] ]; // returns [ ID, name ]
}
});
这篇关于jQuery的自动完成功能使用JSON初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!