如果有人能幫助我使用此代碼,我將非常感激。 我有通過點擊鏈接動態創建的輸入字段。 autosuggest只能在第一個 - 靜態輸入字段上正常工作,但我無法使其在動態輸入字段上工作。動態輸入字段上的自動建議
下面是HTML代碼:
<tr>
<td><label for="addassureed">Additional Assured</label></td>
<td>
<div class="wrapSearch">
<div>
<input type="text" name="addassured[]" class="input1" id="addassured" size="45" maxlength="1000" onkeyup="autoSuggest(this.id, 'listWrap2', 'searchList2', 'addassured', event);" onKeyDown="keyBoardNav(event, this.id);" />
<a href="#" onClick="addInput('dynamicInput');" /><img src="../../img/add.png" height="16" width="16" /></a>
</div>
<div class="listWrap" id="listWrap2">
<ul class="searchList" id="searchList2">
</ul>
</div>
</div>
</td>
創建的輸入域代碼:
var counterAssured = 1;
var limit = 10;
function addInput(divName){
if (counterAssured == limit) {
alert("You have reached the limit of adding " + counterAssured + " inputs");
} else {
var newdiv = document.createElement('div');
newdiv.innerHTML = " <td><label>Additional Assured " + (counterAssured + 1) + "</label></td><td><input type='text' name='addassured["+counterAssured+"]' class='input1'></td>";
document.getElementById(divName).appendChild(newdiv);
counterAssured++;
}
}
以及查詢數據庫的PHP代碼:autosuggest.php
$dbhost = 'localhost'; // Database Host
$dbuser = ''; // Database Username
$dbpass = ''; // Database Password
$dbname = ''; // Database Name
$limit = 20;
if (!isset($_POST['itemCode']))
exit;
$input = trim($_POST['itemCode']);
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname);
$sql = "SELECT company_name FROM contacts WHERE company_name LIKE '".$input."%' LIMIT $limit";
$result = mysql_query($sql);
if (!$result || !mysql_num_rows($result))
exit;
include_once "headers.php";
echo "<response>";
while ($row = mysql_fetch_array($result))
{
$keywords = $row['company_name'];
echo "<keywords>". $keywords ."</keywords>";
}
echo "</response>";
嘗試使用jQuery。 –