gpt4 book ai didi

javascript - 用在特定表格行中选择的相应值填充表格数据

转载 作者:行者123 更新时间:2023-11-28 01:06:27 25 4
gpt4 key购买 nike

我有一个 html 表格,我可以通过单击按钮在其中添加行。现在每个表行包含 8 个 td。基于从表行中的第一个输入框(自动完成)中选择的值,该特定行中的其余 td 将填充数据库中的数据。现在我的代码一切正常,但唯一的问题是这个东西只适用于第一个表行。如果我添加新行并选择新的东西,那么该行中的 td 不会填充数据。我认为问题是我将不得不引用其中的每个 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>

这是我的添加按钮的作用-

$(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 中。这对第一行工作正常,但是当我添加新行时,它不起作用。我已尽力提供尽可能多的信息,如果您需要更多信息,请告诉我。提前致谢。

最佳答案

正如我在评论中所说,ID 必须是唯一的。在您的情况下,当您创建新行时,ID 是相同的并且只采用第一个值(即使您写在第二行、第三行等)。要解决,您需要:1)创建具有唯一ID的输入; 2) 检索并返回数据以正确输入(不仅仅是第一个输入)。

完整和重写的代码:

HTML 端:

<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_1" class="form-control items" autocomplete="off" /></td>
<td><textarea name="size[]" id="size_1" class="form-control size" autocomplete="off"></textarea></td>
<td><input type="text" name="qty[]" id="qty_1" class="form-control qty" autocomplete="off"/></td>
<td><input type="text" name="unit[]" id="unit_1" class="form-control unit" autocomplete="off"/></td>
<td><input type="text" name="price[]" id="price_1" class="form-control price" autocomplete="off"/></td>
<td><input type="text" name="tax[]" id="tax_1" class="form-control tax" autocomplete="off" /></td>
<td><input type="text" name="dis[]" id="dis_1" 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>

JavaScript 方面:

$(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_' + n + '" class="form-control items" autocomplete="off" /></td>' +
'<td><textarea name="size[]" id="size_' + n + '" class="form-control size" autocomplete="off"></textarea></td>' +
'<td><input type="text" name="qty[]" id="qty_' + n + '" class="form-control qty" autocomplete="off"/></td>' +
'<td><input type="text" name="unit[]" id="unit_' + n + '" class="form-control unit" autocomplete="off"/></td>' +
'<td><input type="text" name="price[]" id="price_' + n + '" class="form-control price" autocomplete="off"/></td>' +
'<td><input type="text" name="tax[]" id="tax_' + n + '" class="form-control tax" autocomplete="off" /></td>' +
'<td><input type="text" name="dis[]" id="dis_' + n + '" class="form-control dis" autocomplete="off" /></td>' +
'<td><input type="text" name="stkamt[]" id="amt_' + n + '" class="form-control amt" autocomplete="off" /></td>' +
'<td><button class="btn btn-danger remove">X</button></td>' +
'</tr>';
$('.details').append(tr);
};

$('body').delegate('.items', 'blur', function(e) {
checkitems(e.currentTarget.id.split('_')[1]); // e.currentTarget.id.split('_')[1] - Target element ID
});

function checkitems(targetID)
{
var items = $('#items_' + targetID).val();
if (items != "") {
$.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_' + targetID).val(data.rate);
$('#size_' + targetID).val(data.des);
$('#qty_' + targetID).val(data.qty);
$('#unit_' + targetID).val(data.unit);
} else {
$('#myitemmodal').modal("show");
}
}
});
}
};

在这种情况下,我重写了代码,以便每个输入都具有附加到其 ID 的唯一属性(在这种情况下,表中的行数,例如,unit_1 而不是 unit)。

关于javascript - 用在特定表格行中选择的相应值填充表格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com