以发票形式在每个td中生成id

时间:2015-04-18 18:02:09

标签: javascript jquery

我正在使用发票表单,当我尝试编辑发票表单时,我不知道如何在每个td中生成ID ...因为我使用ajax来调用不同的数据当我需要更改某些产品时,每个示例中的第二个或第三个"td"总是更改第一个"td" ...

发票表格的一部分:

<td><input type="text" name="cat[]" id="cat_(HERE I NEED PUT A ID)" class="form-control"></td>
<td><input type="text" name="subcat[]" id="subcat_(HERE I NEED PUT A ID)" class="form-control"></td>
<td><input type="text" name="vineta[]" id="vineta_(HERE I NEED PUT A ID)" class="form-control"></td>

2 个答案:

答案 0 :(得分:0)

你几乎肯定不会在每个id元素上想要 td,但如果你真的这样做,可以采用以下方式:

// Spin through all rows in the table
$("#yourtable tr").each(function(row) {
    // Spin through all cells in the row
    $(this).find("td").each(function(col) {
        // Assign an ID
        this.id = "cell" + row + "x" + col;
    });
});

...其中#yourtable只是与您的表匹配的选择器的占位符。假设您没有任何嵌套表格。

但是,你可能不会那么想。你还没有提供有关触发ajax调用的详细信息,但通常在出现这种情况时,它与用户对你想要的行中元素的操作有关所做的更改(例如,增加或减少计数)。如果这是您的用例,请记住该行,然后更新该行中的单元格 ,而不是整体搜索该文档。

示例:

&#13;
&#13;
$("table").on("click", "input[type=button]", function() {
  var $btn = $(this),
      operation = $btn.val() == "+" ? 1 : -1,
      $row = $btn.closest('tr');
  
  // Simulating ajax by using setTimeout...
  setTimeout(function() {
    var $input = $row.find('input[name="foo[]"]');
    $input.val(+$input.val() + operation);
  }, 10);
});
&#13;
<table>
  <tbody>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
  </tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

最后我在帖子answer for xdazz

后找到了答案

代码:

$sql = "SELECT @i := @i + 1 AS i, cod, nombreProd, .....FROM PRODUCTS, (SELECT @i:=0) b WHERE .... ";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo $i = $row['i'];
$cod = $row['cod'];
....

在每个tr的内部&gt; td&gt;输入:

<td><input type="text" data-type="cod" name="cod[]" value="<?php echo $cod; ?>" id='cod_<?php echo $i; ?>' minlength="2" class="form-control autocomplete_txt required" autocomplete="off" required></td>