回答
如果您熟悉使用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. 加入列表框選擇到Sqlite3連接輸入框
- 2. 選擇框打開輸入框jQuery
- 3. 如何從輸入框中彈出選擇列表?
- 4. jquery選擇器 - 分配值選擇輸入文本框之前
- 5. 列表框輸入舊選擇不新選擇到SQLServer更新
- 6. 使用輸入鍵選擇複選框
- 7. 只選擇getElementsByTagName中的複選框輸入,排除文本框輸入
- 8. 多個複選框的輸入值選擇爲單個文本框vba
- 9. 將複選框輸入值放入複選框本身
- 10. 找出與jQuery的選擇框的值
- 11. 基於下拉框選擇輸出隱藏輸入
- 12. 無法爲動態輸入ID值選擇複選框
- 13. While循環輸出清空選擇框
- 14. 播放框架選擇輸入驗證
- 15. jquery - 選擇和輸入框組合
- 16. 輸入框移動時選擇
- 17. 複選框和輸入字段的值
- 18. angularjs列表框中的值選擇
- 19. jQuery選擇框值
- 20. 根據另一個選擇框的輸入動態添加選擇框
- 21. 哪個是文本框中數值輸入的最佳選擇?
- 22. 克隆和選擇框onchange輸入文本的更改值
- 23. 爲什麼輸入和選擇框在他們的價值觀
- 24. 列表框選擇
- 25. 多列選擇框
- 26. Javascript列選擇框
- 27. 複選框+列表框選擇
- 28. 引導組合框,選擇鍵入值
- 29. laravel選擇框填入默認值
- 30. HTML複選框,輸入沒有出現
嗨吉姆!以及我不是在jQuery的專家我試圖使用一個插件,但不工作正確,如果你有一個例子會很好 – user505790 2011-04-18 13:12:15
給我15分鐘,我會張貼它作爲你的另一個答案。 – 2011-04-18 13:17:27
您也可以通過對POST變量進行硬編碼並直接從瀏覽器窗口調用它來測試AJAXCODE.PHP腳本。 – 2011-04-18 13:41:41