我一直在尝试寻找解决方案 1 或 2 天,但无法让它发挥作用..
我有一个 id="items" 的表,每一行都有“item-row”类。我在 articlename 字段上使用了自动完成功能(它是一个发票表)。这很好..现在当我想添加一个新行并触发这些字段的自动完成时,它不起作用..该行不会被添加....
// Get the table object to use for adding a row at the end of the table
var $itemsTable = $('#items');
// Create an Array to for the table row. ** Just to make things a bit easier to read.
var rowTemp = [
'<tr class="item-row"><td class="item-name"><div class="delete-wpr"><textarea name="articleName[]" id="articleName"></textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td><td class="description"><textarea name="articleDescription[]" id="articleDescription"></textarea></td><td><textarea name="articlePrice[]" id="articlePrice" class="cost">0.00</textarea></td><td><textarea name="articleQty[]" id="articleQty" class="qty">0</textarea></td><td><span class="price">0.00</span></td></tr>'
].join('');
// Add row to list and allow user to use autocomplete to find items.
$("#addrow").bind('click',function(){
var $row = $(rowTemp);
// save reference to inputs within row
var $itemCode = $row.find('#articleName');
var $itemDesc = $row.find('#articleDescription');
var $itemPrice = $row.find('#articlePrice');
var $itemQty = $row.find('#articleQty');
if ( $('#articleName:last').val() !== '' ) {
// apply autocomplete widget to newly created row
$row.find('#articleName').autocomplete({
source: 'getproducts.php',
minLength: 1,
select: function(event, ui) {
$itemCode.val(ui.item.itemCode);
$itemDesc.val(ui.item.itemDesc);
$itemPrice.val(ui.item.itemPrice);
// Give focus to the next input field to recieve input from user
$itemQty.focus();
return false;
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.itemCode + " - " + item.itemDesc + "</a>" )
.appendTo( ul );
};
// Add row after the first row in table
$('.item-row:last', $itemsTable).after($row);
$($itemCode).focus();
} // End if last itemCode input is empty
return false;
});
A 链接的 id="addrow"
顺便说一句,如果我使用以下代码,它确实会添加一行,但不会自动完成:
$("#addrow").bind('click',function(){
$(".item-row:last").after('<tr class="item-row"><td class="item-name"><div class="delete-wpr"><textarea name="articleName[]" id="articleName"></textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td><td class="description"><textarea name="articleDescription[]" id="articleDescription"></textarea></td><td><textarea name="articlePrice[]" id="articlePrice" class="cost">0.00</textarea></td><td><textarea name="articleQty[]" id="articleQty" class="qty">0</textarea></td><td><span class="price">0.00</span></td></tr>');
if ($(".delete").length > 0) $(".delete").show();
bind();
});
我希望有人能照亮我或指出我正确的方向..非常感谢。