简体   繁体   English

在jQuery中获取已编辑的TD值

[英]Get Edited TD values in jQuery

I have an HTML table whose span is populed by PHP variables. 我有一个HTML表,其范围由PHP变量填充。 The table is showing up pretty well, showing the data present in the database. 该表显示得很好,显示了数据库中存在的数据。 The following is the table row, which is wrapped inside a PHP while loop. 以下是表格行,该行包装在PHP while循环中。

<table class="table table-striped table-hover table-bordered dataTable no-footer" id="sample_editable_1" role="grid" aria-describedby="sample_editable_1_info">
  <tr role="row" class="odd">
    <td contenteditable="false" class="sorting_1">
      <span class="sl"> </span>
    </td>
    <td contenteditable="false">
      <span class="itid"><?php echo $itid1; ?></span>
    </td>
    <td contenteditable="false">
      <span class="name"><?php echo $name1; ?></span>
    </td>
    <td contenteditable="false">
      <span class="price"> <?php echo $price1; ?></span>
    </td>
    <td contenteditable="false">
      <span class="qty"><?php echo $qty1; ?></span>
    </td>
    <td>
      <button class="pr_edit" value="edit" href="javascript:;"> Edit </button>
    </td>
    <td>
      <button class="pr_elete" href="javascript:;"> Delete </button>
    </td>
  </tr>
</table>

The following is jQuery function I am using to pull the Data. 以下是我用来提取数据的jQuery函数。

$('#sample_editable_1').on('click', '.pr_edit', function() {
  console.log("here");
  var currentTD = $(this).parents('tr').find('td');
  $.each(currentTD, function() {
    $(this).prop('contenteditable', true);
  });

  var ino = $(this).closest('tr').find("span.itid").text();
  var iname = $(this).closest('tr').find("span.name").text();
  var iprice = $(this).closest('tr').find("span.price").text();
  var iqty = $(this).closest('tr').find("span.qty").text();
});

However, I am not able to retrieve the values from the fields that are edited. 但是,我无法从已编辑的字段中检索值。 The rest of the TR cells are Okay. 其余的TR单元都可以。 How do I retrieve modified values from the Table cells I have edited? 如何从已编辑的表格单元中检索修改后的值?

IMPORTANT NOTE: 重要的提示:

I am able to obtain values that are from unedited TR cells. 我能够从未经编辑的TR单元获取值。 The jQuery selectors are correct. jQuery选择器是正确的。

When you set contenteditable to true, you can store the current value in with the jQuery data() function. contenteditable设置为true时,可以使用jQuery data()函数存储当前值。

For example: 例如:

$(this).data('original-value', $(this).text());

Then, when it's time to read all of the values in the table, you can compare the current value of the td content to the original value. 然后,当需要读取表中的所有值时,可以将td内容的当前值与原始值进行比较。 The implementation would be something like this: 实现将是这样的:

$('#sample_editable_1 td').each(function() {
  if ($(this).data('original-value') !== $(this).text()) {
    alert('this cell has been edited');
    alert('original value was: ' + $(this).data('original-value'));
    alert('new value is: ' + $(this).text());
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM