如果有人可以帮我解决这个问题,我真的很感激。 我有通过点击链接动态创建的输入字段。 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++;
}
}
用于查询db的aut代码: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>";
答案 0 :(得分:1)
你错过了这两个事件[onkeyup&amp; onKeyDown]在动态创建的字段中
试试这个,
newdiv.innerHTML = ' <td><label>Additional Assured ' + (counterAssured + 1) + '</label></td><td><input type="text" name="addassured['+counterAssured+']" class="input1" onkeyup="autoSuggest(this.id, \'listWrap2\', \'searchList2\', \'addassured\', event);" onKeyDown="keyBoardNav(event, this.id);"></td>';
或使用备用引用语法:
newdiv.innerHTML = " <td><label>Additional Assured " + (counterAssured + 1) + "</label></td><td><input type=\"text\" name=\"addassured["+counterAssured+"]\" class=\"input1\" onkeyup=\"autoSuggest(this.id, 'listWrap2', 'searchList2', 'addassured', event);\" onKeyDown=\"keyBoardNav(event, this.id);\"></td>";