jqueryui 自动完成自定义数据、列表创建
我正在构建一个表单,其中第一个字段限制第二个字段中的可用内容,第二个字段限制第三个字段中的可用内容。
我正在尝试使用 Jqueryui 自动完成功能,但遇到了问题。我在网上尝试了许多其他来源,但似乎无法接受。我是自定义小部件的新手,这可能可以解释问题。
目前,我能够正确地发布和接收来自我的 php 文件(见下文)的数据,但自动完成功能尚未显示它找到的信息。数据就在那里,我只是无法将其放入弹出列表中。
想法?
$(".tiers input[type='text']").autocomplete({
source: function( request, response )
{
var $form_data=$('.tiers').parents('form').serialize();
$.ajax({
url: "issue_autocomplete.php",
type: "POST",
dataType: "json",
data:$form_data,
success: function(data){
response($.map( data, function(item){
return{
label:item.tier1,
value:item.tier1
}
}))
}
});
},
minLength: 2
});
还有 php(它可以很好地检索信息)
$tier1=mysql_real_escape_string($_POST['tier1']);
$tier2=mysql_real_escape_string($_POST['tier2']);
$tier3=mysql_real_escape_string($_POST['tier3']);
if($tier1!=''){
$query = mysql_query("SELECT * FROM varIssues WHERE tier1 LIKE '$tier1%'");
}
if($tier2!=''){
$query = mysql_query("SELECT * FROM varIssues WHERE tier1='$tier1' AND tier2 LIKE '$tier2%'");
}
if($tier3!=''){
$query=mysql_query("SELECT * FROM varIssues WHERE tier1 = '$tier1' AND tier2 ='$tier2' AND tier3 LIKE '$tier3%'");
}
//build array of results
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$issues[$x] = array('tier1'=>$row["tier1"],'tier2'=>$row['tier2'],'tier3'=>$row['tier3']);
}
//echo JSON to page
$response = $_GET["callback"] . "(" . json_encode($issues) . ")";
echo $response;
I'm building a form where the first field restricts what's available in the second, and the second restricts what's available in the third.
I'm trying to use Jqueryui autocomplete for this, but am running into an issue. I've tried a number of other sources online but can't seem to get it to take. I am new to customizing widgets, which may explain the problem.
Currently, I am able to properly post and receive data from my php file (found below), but the autocomplete doesn't yet display the information it finds. The data is there, I am simply unable to get it into the pop-down list.
Thoughts?
$(".tiers input[type='text']").autocomplete({
source: function( request, response )
{
var $form_data=$('.tiers').parents('form').serialize();
$.ajax({
url: "issue_autocomplete.php",
type: "POST",
dataType: "json",
data:$form_data,
success: function(data){
response($.map( data, function(item){
return{
label:item.tier1,
value:item.tier1
}
}))
}
});
},
minLength: 2
});
And the php (which is retrieving information just fine)
$tier1=mysql_real_escape_string($_POST['tier1']);
$tier2=mysql_real_escape_string($_POST['tier2']);
$tier3=mysql_real_escape_string($_POST['tier3']);
if($tier1!=''){
$query = mysql_query("SELECT * FROM varIssues WHERE tier1 LIKE '$tier1%'");
}
if($tier2!=''){
$query = mysql_query("SELECT * FROM varIssues WHERE tier1='$tier1' AND tier2 LIKE '$tier2%'");
}
if($tier3!=''){
$query=mysql_query("SELECT * FROM varIssues WHERE tier1 = '$tier1' AND tier2 ='$tier2' AND tier3 LIKE '$tier3%'");
}
//build array of results
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$issues[$x] = array('tier1'=>$row["tier1"],'tier2'=>$row['tier2'],'tier3'=>$row['tier3']);
}
//echo JSON to page
$response = $_GET["callback"] . "(" . json_encode($issues) . ")";
echo $response;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个 jQuery 插件可能对您有帮助。
http://www.code assembly.com/Simple-chained-combobox- jQuery 插件/
This jQuery plug-in may be helpful to you.
http://www.codeassembly.com/Simple-chained-combobox-plugin-for-jQuery/
响应末尾加分号可以吗?
编辑:也许是解析结果的另一种方法(假设您关于返回数据的声明是正确的 - 取消注释下面的alert()以确保确定。
With the semicolon at the end of the response does it work?
Edit: another way to parse out the results perhaps (assumption your statement about the returned data IS correct - uncomment the alert() below to know for sure.
我认为,如果这是您发回的 json
您应该像这样访问它
在任何情况下,如果您安装了 firebug,您可以 console.log 返回的数据并检查一切是否正常
I think that if this is the json you are sending back
You should access it like this
In any cas if you have firebug installed you could console.log the data returned and check that everything is ok