2013-01-16 58 views
0

如果有人能幫助我使用此代碼,我將非常感激。 我有通過點擊鏈接動態創建的輸入字段。 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>"; 
+0

嘗試使用jQuery。 –

回答

1

你錯過了這兩個事件[的onkeyup &的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>"; 
+1

對不起,雖然JS同時支持'''和''',但html不會,因此html引號必須是''''。我已經爲你解決了你的答案。 +1 – Basic

+1

謝謝。他用單引號。這就是爲什麼我不想改變,並與他的格式本身。無論如何感謝您的建議。 –

+0

謝謝你們的幫助。我已經嘗試過這種改變,但仍然不起作用。我認爲它應該做的東西與div:

Lilou

相關問題