【问题标题】:Fill the table data with corresponding value selected in specific table row用在特定表格行中选择的相应值填充表格数据
【发布时间】:2017-01-25 12:22:17
【问题描述】:

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

【问题讨论】:

  • IDs 必须是唯一的。只能填写第一行。有什么建议吗?您可以为每个新行添加唯一 id(例如,使用数字)或编写一些不同的 JS 函数。
  • 在什么意义上?你能详细说明一下吗。@EdvinTenovimas
  • 我可以尝试用修改后的代码回答您的问题来解决这个问题。
  • 当然...那很好..谢谢

标签: javascript php jquery html mysql


【解决方案1】:

正如我在 cmets 中所说,IDs 必须是唯一的。在您的情况下,当您创建新行时,IDs 是相同的,并且仅采用第一个值(即使您在第二、第三等行中写入)。要解决此问题,您需要: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)。

【讨论】:

  • 很高兴你能解决这个问题!此外,如果可以的话,您也可以考虑对答案进行投票(没有义务这样做,但这确实对我有帮助)。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 2014-12-11
  • 2021-09-21
相关资源
最近更新 更多