0

请帮我解决一下这个。我有一个这样的xml文件

<ProjectList>
  <ProjectName>AAJ</ProjectName>
  <ProjectName>hjgg</ProjectName>
  <ProjectName>asfasf</ProjectName>
  <ProjectName>qwere</ProjectName>
  <ProjectName>sadaf</ProjectName>
  <ProjectName>asd</ProjectName>
  <ProjectName>asad</ProjectName>
  <ProjectName>wildzike</ProjectName>
  <ProjectName>yahoo</ProjectName>
</ProjectList>

我需要一个 php 中的搜索框来进行实时搜索。我从这个链接http://www.w3schools.com/php/php_ajax_livesearch.asp得到了解决方案来创建那个搜索框。但是这里不方便,因为没有使用箭头键选择值的选项。我认为使用标签将搜索文本字段替换为下拉字段会更好。如何使用<select>xml 文件中的数据(应该与 xml 文件匹配)填充选择,以便我可以使用 up 和 down 选择值箭头键。请建议我提供示例代码,因为我是 php 和 Jquery 的初学者。

谢谢。

4

2 回答 2

0

我将使用 Jquery UI 自动完成提供 ua 整洁的解决方案

$("#search").autocomplete({
minglength:2 // Just to prevent many useless requests
source: "livesearch.php"
select : function (e,ui){
window.href=ui.item.val()}

 });

现在是php端

$q=$_GET["term"]; Note that Jquery sends the search query as GET using term variable

//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
// Some Code for hint note that it must return a json or array of json for many results
}
    echo $hint;
?>

Json结构是这样的

{"value":"The value you want","label":"The display name you want to show useless in ur case"}
于 2013-07-08T10:06:29.360 回答
0

这是一种填充<select>

它是为在现代浏览器上工作而编写的。polyfill 需要更多代码。

x.xml替换为搜索 url。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<script>
var
x=function(a,b,c){c=new XMLHttpRequest;c.open('GET',a);c.onload=b;c.send()};
 x('x.xml',function(){
  var
  xml=this.responseXML.firstChild.getElementsByTagName('ProjectName'),
  xmll=xml.length,
  a=[],
  sel=document.createElement('select');
  while(xmll--){
   var txt=xml[xmll].textContent;
   a[xmll]='<option value="'+txt.trim()+'">'+txt+'</option>';
  }
  sel.innerHTML=a.join('');
  document.body.appendChild(sel);
 });
</script>
<body>
</body>
</html>
于 2013-07-08T09:34:40.780 回答