我有一個html表格,其中我可以通過點擊一個按鈕添加行。現在每個表格行都包含8個td。根據從表格行的第一個輸入框(自動完成)中選擇的值,該特定行中其餘的td被填充來自數據庫的數據。現在一切工作正常,我的代碼,但唯一的問題是,這個東西只適用於第一個錶行。如果我添加新行並選擇新的東西,然後在該行TD沒有得到填充data.I認爲問題是我將不得不引用每個tr和td,並通過它們循環代碼。但我無法做到這一點,請幫助我。 這裏是代碼 -填寫表格數據與在特定表格行中選擇的對應值
<table class="table table-bordered" id="stock" >
<thead>
<tr>
<td>Sr no.</td>
<td>Product Name</td>
<td>Description</td>
<td>Qty</td>
<td>Unit</td>
<td>Rate</td>
<td>Tax</td>
<td>Dis</td>
<td>Total</td>
<td><input type="button" name="add" id="add" value="+" class="btn btn-warning" /></td>
</tr>
</thead>
<tbody class="details">
<tr>
<td class="no">1</td>
<td><input type="text" name="items[]" id="items" class="form-control items" autocomplete="off" /></td>
<td><textarea name="size[]" id="size" class="form-control size" autocomplete="off"></textarea></td>
<td><input type="text" name="qty[]" id="qty" class="form-control qty" autocomplete="off"/></td>
<td><input type="text" name="unit[]" id="unit" class="form-control unit" autocomplete="off"/></td>
<td><input type="text" name="price[]" id="price" class="form-control price" autocomplete="off"/></td>
<td><input type="text" name="tax[]" id="tax" class="form-control tax" autocomplete="off" /></td>
<td><input type="text" name="dis[]" id="dis" class="form-control dis" autocomplete="off" /></td>
<td><input type="text" name="stkamt[]" id="amt" class="form-control amt" autocomplete="off" /></td>
<td><button class="btn btn-danger remove">X</button></td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
這裏是我的添加按鈕does-
$(function(){
$('#add').click(function(){
addnewrow();
});
function addnewrow(){
var n = ($('.details tr').length - 0)+1;
var tr = '<tr>'+
'<td class="no">'+n+'</td>'+
'<td><input type="text" name="items[]" id="items" class="form-control items" autocomplete="off" /></td>'+
'<td><textarea name="size[]" id="size" class="form-control size" autocomplete="off"></textarea></td>'+
'<td><input type="text" name="qty[]" id="qty" class="form-control qty" autocomplete="off"/></td>'+
'<td><input type="text" name="unit[]" id="unit" class="form-control unit" autocomplete="off"/></td>'+
'<td><input type="text" name="price[]" id="price" class="form-control price" autocomplete="off"/></td>'+
'<td><input type="text" name="tax[]" id="tax" class="form-control tax" autocomplete="off" /></td>'+
'<td><input type="text" name="dis[]" id="dis" class="form-control dis" autocomplete="off" /></td>'+
'<td><input type="text" name="stkamt[]" id="amt" class="form-control amt" autocomplete="off" /></td>'+
'<td><button class="btn btn-danger remove">X</button></td>'+
'</tr>';
$('.details').append(tr);
}
這裏是當我在第一個TD自動完成下拉列表中選擇一個特定的項目,並轉到接下來會發生什麼現場
$('body').delegate('.items','blur',function(){
checkitems();
});
function checkitems(){
var items = $('#items').val();
if(items == ""){
}else{
$.ajax({
type: 'post',
url: 'itemcheck.php',
data: {key: items},
dataType:'json',
success: function(data) {
if(data){
var tr = $(this).closest('tr'); // here is the code for retrieving the values. i think here i need to make some changes
$('#price').val(data.rate);
$('#size').val(data.des);
$('#qty').val(data.qty);
$('#unit').val(data.unit);
}
else
{
$('#myitemmodal').modal("show");
}
}
});
}
}
這裏item_check.php如果該項目已經存在與否。如果它不存在檢查,它會顯示一個新的項目對話框中,如果確實如此,那麼它去ITEM_ value.php來檢索其餘的值,並將它們顯示在表格行的td中。這個東西對第一行工作正常,但是當我添加新行時,它不起作用。 我試圖提供儘可能多的信息,請讓我知道如果你需要更多的東西。提前感謝。
'IDs'必須是唯一的。只有第一行可以填寫。建議?你可以爲每一個新行添加唯一的id(例如數字)或者寫一個不同的'JS'函數。 –
什麼意思?你可以請詳細說明@ EdvinTenovimas –
我可以嘗試通過修改代碼來回答你的問題。 –