回答
如果您熟悉使用jQuery和Ajax,这可以非常简单地进行。您可以观看文本框的输入,并在达到您满意的阶段时触发Ajax请求。 Ajax查询的返回是它们用于填充下拉框。
下面是一个在实践中的例子。你需要在你的服务器上有jQuery,或者引用CDN中的副本(如Google)。为了保护无辜者,我不得不编辑它,但是有更多的线条可以正常工作。
您需要弄清楚自己的唯一项目,它应该通过什么方式调用Ajax,因此可以填充下拉列表。我已经在很多角色之后使用过它,一旦识别出了文字图案,并且还有一个hoover off事件。这是你的选择。
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>
HTML代码
<input type="text" id="CARS" value="" >
<select id="CARSLOOKUP">
</select>
AJAXCODE.PHP CODE
<?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
?>
jQuery用户界面具有自动完成模块:
嗨亚当,以及选择框必须是独立的,因为我需要的输入值和选择框值 – user505790 2011-04-18 13:10:17
是的,你可以用jQuery UI来做到这一点。试试这个演示吧,试试吧。 – 2011-04-18 13:11:49
- 1. 选择框值复制输入点击
- 2. 加入列表框选择到Sqlite3连接输入框
- 3. 选择框打开输入框jQuery
- 4. 根据选择的下拉列表的值添加输入框
- 5. 从复选框选择插入排序的输入文本框
- 6. 列表框多值选择
- 7. 如何从输入框中弹出选择列表?
- 8. jquery选择器 - 分配值选择输入文本框之前
- 9. 列表框输入旧选择不新选择到SQLServer更新
- 10. 使用输入键选择复选框
- 11. 只选择getElementsByTagName中的复选框输入,排除文本框输入
- 12. 的attachEvent输入和选择框
- 13. 多个复选框的输入值选择为单个文本框vba
- 14. 选择加入/退出复选框
- 15. 将复选框输入值放入复选框本身
- 16. 找出与jQuery的选择框的值
- 17. 基于下拉框选择输出隐藏输入
- 18. 无法为动态输入ID值选择复选框
- 19. While循环输出清空选择框
- 20. 乘复选框值与输入框值 - Jquery的
- 21. 播放框架选择输入验证
- 22. 一起选择菜单和输入框
- 23. jquery - 选择和输入框组合
- 24. jQuery:选择一个文本输入框
- 25. 输入框移动时选择
- 26. 多个选择到输入框
- 27. 复选框和输入字段的值
- 28. JQuery的:分配选择框的值输入字段
- 29. angularjs和jQueryUI的日期选择器输入框的值
- 30. angularjs列表框中的值选择
嗨吉姆!以及我不是在jQuery的专家我试图使用一个插件,但不工作正确,如果你有一个例子会很好 – user505790 2011-04-18 13:12:15
给我15分钟,我会张贴它作为你的另一个答案。 – 2011-04-18 13:17:27
您也可以通过对POST变量进行硬编码并直接从浏览器窗口调用它来测试AJAXCODE.PHP脚本。 – 2011-04-18 13:41:41