【发布时间】:2012-09-22 09:16:33
【问题描述】:
我有一个 html 表格,我想在每一行上添加一个删除按钮。 当用户点击它时,我想做两件事:
1) 从表中删除该行。 2)从被删除的行中提取数据 - 一个特定的单元格,然后使用该值对将从数据库中删除记录的函数进行 ajax 调用。该函数返回一个新的项目列表,然后我将使用它来重新显示同一个表格。
我在 stackoverflow 上找到了其他人的以下帖子: How to remove current row from table in jQuery?
所以我猜这是第一点。但我不知道如何修改帖子所选答案中的代码以允许抓取行中的 id 值,然后进行 ajax 调用。
<table class="table table-bordered table-striped" id="databaserecords">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
<th>Voice</th>
<th>Jumbo</th>
<th>Mode</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php foreach ($dblist as $record): ?>
<tr>
<td class ='recordId'><?php echo $record['Id'] ?></td>
<td class ='recordName'><?php echo $record['Name'] ?></td>
<td><?php echo $record['Status'] ?></td>
<td><?php echo $record['Voice'] ?></td>
<td><?php echo $record['Jumbo'] ?></td>
<td class='recordmode'><?php echo $record['Mode'] ?></td>
<td><button id="deleteRecord">Delete Record</button></td>
</tr>
<?php endforeach ?>
<script>
$('#deleteRecord').live('click', function() {
//get a count of all records. only allowed to delete if you have more than one record.
var recCount = $('#databaserecords tbody tr').length;
if (recCount > 1)
{
$thevalueIwanttoSave = $(this).closest('recordId').val();
alert($thevalueIwanttoSave);
}
});
</script>
现在,警报总是说我的变量未定义。 你能为我指出正确的方向吗?我确实知道如何编写 ajax 调用...我想我只需要帮助来从表中获取值。
谢谢。
【问题讨论】:
标签: php jquery dom-manipulation