Php 列出选择框值的输入框
当我在输入文本(如:cars)中键入文本,并且在选择框中列出汽车名称(宝马、雪佛兰等)时,有人能帮我找到一种方法吗Php 列出选择框值的输入框,php,jquery,mysql,Php,Jquery,Mysql,当我在输入文本(如:cars)中键入文本,并且在选择框中列出汽车名称(宝马、雪佛兰等)时,有人能帮我找到一种方法吗 有人有想法或例子吗?jQuery UI有一个自动完成模块: 如果您熟悉jQuery和Ajax,这可以非常简单地执行。您观察文本框的输入,并在Ajax请求达到您满意的阶段时发出请求。Ajax查询的返回用于填充下拉框 下面是实践中的一个例子。您需要在服务器上安装jquery,或者引用CDN(如Google)中保存的副本。为了保护无辜,我不得不对它进行了一些编辑,但是再多写几行就行了 您
有人有想法或例子吗?jQuery UI有一个自动完成模块:
如果您熟悉jQuery和Ajax,这可以非常简单地执行。您观察文本框的输入,并在Ajax请求达到您满意的阶段时发出请求。Ajax查询的返回用于填充下拉框 下面是实践中的一个例子。您需要在服务器上安装jquery,或者引用CDN(如Google)中保存的副本。为了保护无辜,我不得不对它进行了一些编辑,但是再多写几行就行了 您需要自己弄清楚的唯一一项是,应该通过什么方式调用Ajax,从而填充您的下拉列表。我在很多字符之后使用过它,一旦一个文本模式被识别,以及一个胡佛事件。选择权在你 JAVASCRIPT
<script language="JavaScript" src="./js.jquery.inc.js"></script>
<script language="JavaScript" type="text/javascript">
// This is the pure ajax call, needs some way of being called.
$.ajax({
url: "ajaxCode.php",
cache: false,
type: "POST",
data: ({carID : $("#CARS").val()}),
dataType: "xml",
success: function(data){
populateCars(data);
},
error: function(data){
// Something in here to denote an error.
alert("Error no cars retrieved.");
}
});
function populateCars(data)
{
$(data).find("node").each(function()
{
var carStr = $(this).find("String").text()
var carKey = $(this).find("Key").text()
$("<option value='"+carKey+"'>"+carStr+"</option>").appendTo("#CARSLOOKUP");
});
}
</script>
//这是纯ajax调用,需要某种调用方式。
$.ajax({
url:“ajaxCode.php”,
cache:false,
类型:“POST”,
数据:({carID:$(“#CARS”).val(),
数据类型:“xml”,
成功:功能(数据){
大众汽车(数据);
},
错误:函数(数据){
//在这里表示错误的东西。
警报(“错误-没有检索到车辆”);
}
});
函数populateCars(数据)
{
$(数据)。查找(“节点”)。每个(函数()
{
var carStr=$(this.find(“String”).text()
var carKey=$(this.find(“Key”).text()
$(“+carStr+”)。附加到(“#CARSLOOKUP”);
});
}
HTML代码
<input type="text" id="CARS" value="" >
<select id="CARSLOOKUP">
</select>
AJAXCODE.PHP代码
<?php
// Post code will come in as a POST variable!
$carCode = $_POST['carID'];
// Need to build up an array of cars to return, based upon the example below.
// Preferably based upon the input given within the car code variable.
foreach($carList as $cars)
{
$returnArray[] = array("Key" => "Vectra", "String" => "Vauxhall Vectra");
}
// Output the headers and data required.
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/xml');
// This simply converts the array and returns formatted XML data (functions listed below).
$xml = new array2xml('cars');
$xml->createNode($returnArray);
echo $xml;
exit(0);
class array2xml extends DomDocument
{
public $nodeName;
private $xpath;
private $root;
private $node_name;
/**
* Constructor, duh
*
* Set up the DOM environment
*
* @param string $root The name of the root node
* @param string $nod_name The name numeric keys are called
*
*/
public function __construct($root='root', $node_name='node')
{
parent::__construct();
/*** set the encoding ***/
$this->encoding = "ISO-8859-1";
/*** format the output ***/
$this->formatOutput = true;
/*** set the node names ***/
$this->node_name = $node_name;
/*** create the root element ***/
$this->root = $this->appendChild($this->createElement( $root ));
$this->xpath = new DomXPath($this);
}
/*
* creates the XML representation of the array
*
* @access public
* @param array $arr The array to convert
* @aparam string $node The name given to child nodes when recursing
*
*/
public function createNode( $arr, $node = null)
{
if (is_null($node))
{
$node = $this->root;
}
foreach($arr as $element => $value)
{
$element = is_numeric( $element ) ? $this->node_name : $element;
$child = $this->createElement($element, (is_array($value) ? null : $value));
$node->appendChild($child);
if (is_array($value))
{
self::createNode($value, $child);
}
}
}
/*
* Return the generated XML as a string
*
* @access public
* @return string
*
*/
public function __toString()
{
return $this->saveXML();
}
/*
* array2xml::query() - perform an XPath query on the XML representation of the array
* @param str $query - query to perform
* @return mixed
*/
public function query($query)
{
return $this->xpath->evaluate($query);
}
} // end of class
?>
createNode($returnArray);
echo$xml;
出口(0);
类array2xml扩展了DomDocument
{
公共$nodeName;
私用$xpath;
私有$root;
私有$node_名称;
/**
*建造师,嗯
*
*设置DOM环境
*
*@param string$root根节点的名称
*@param string$nod_name调用名称数字键
*
*/
公共函数构造($root='root',$node\u name='node')
{
父项::_构造();
/***设置编码***/
$this->encoding=“ISO-8859-1”;
/***格式化输出***/
$this->formatOutput=true;
/***设置节点名称***/
$this->node\u name=$node\u name;
/***创建根元素***/
$this->root=$this->appendChild($this->createElement($root));
$this->xpath=newdomxpath($this);
}
/*
*创建数组的XML表示形式
*
*@access-public
*@param array$arr要转换的数组
*@aparam string$node递归时给子节点的名称
*
*/
公共函数createNode($arr,$node=null)
{
if(is_null($node))
{
$node=$this->root;
}
foreach($arr作为$element=>$value)
{
$element=is_numeric($element)?$this->node_name:$element;
$child=$this->createElement($element,(is_数组($value)?null:$value));
$node->appendChild($child);
if(是_数组($value))
{
self::createNode($value,$child);
}
}
}
/*
*以字符串形式返回生成的XML
*
*@access-public
*@返回字符串
*
*/
公共函数
{
返回$this->saveXML();
}
/*
*array2xml::query()-对数组的XML表示形式执行XPath查询
*@param str$query-要执行的查询
*@返回混合
*/
公共函数查询($query)
{
返回$this->xpath->evaluate($query);
}
}//下课
?>
Hi adam,选择框需要分开,因为我需要输入值和选择框值是的,你可以用jQuery UI来实现这一点。试一试演示,试一试。嗨,吉姆!我不是jquery方面的专家,我尝试过使用一个插件,但效果不好,如果你有一个例子的话。给我15分钟,我会把它作为另一个答案发布给你。你也可以通过简单地硬编码post变量并直接从浏览器窗口调用它来测试AJAXCODE.PHP脚本。谢谢亲爱的,我会尝试一下让你知道这是你的时间