我有一個問題,當使用jQuery克隆時,試圖將jQuery AutoComplete應用於表中的多行。 AutoComplete在第一行工作,但在將其他行添加到表時失敗。到目前爲止,我有以下幾點:JQuery AutoComplete和Clone
HTML表:
<table class="table" cellspacing="0" id="myTable">
<tr>
<th width="40%">Item</th>
<th width="60%">Description</th>
</tr>
<tr>
<td>input name="product_title" id="product_title" type="text"><td>
<td><textarea name="product_description" id="product_description"></textarea></td>
</tr>
</table>
<input type="button" value="Add Row" onclick="javascript:addRow()">
克隆腳本:
function addRow(){
$('#myTable tr:last').clone(true).insertAfter('#myTable tr:last');
$('#myTable tr:last input').val("");
$('#myTable tr:last input:first').focus();
}
自動完成腳本:
$().ready(function() {
$("#product_title").autocomplete(products, {
width: 380,
matchContains: "word",
formatItem: function(row) {
return row.title;
}
});
$('#product_title').result(function(event, data) {
$('#product_description').val(data.description);
});
});
爲自動完成數據從拉一個簡單的MySQL查詢,它定義了產品標題和描述。
目前,添加新行可以正常工作,對於表的第一行AutoComplete也會正常工作,但是無法在添加的任何其他行上工作。即使我手動添加第二行到HTML表格,AutoComplete也無法在此上工作。
有沒有人知道是否有一個(簡單)的方法來修改上述代碼,使其工作?當談到jQuery時,我是新手,所以越詳細,越好。
在此先感謝!
嗨Charlietfl。非常感謝您的詳細回覆,這對我幫助很大!我改變了我的輸入字段,讓class =「product_title」和class =「product_description」,並且我還將我的代碼更改爲上面的代碼。 AutoComplete現在適用於所有新添加的字段,但唯一的問題是,當我在新添加的行中鍵入產品標題時,產品說明會在所有行上發生變化。你知道這可能是什麼原因嗎?再次感謝! – mckeegan375