我有一个来自通过PHP循环构建的数据库的记录列表。问题是,我需要一种方法来删除记录(不刷新页面将是最好的),但我很难弄清楚如何做到这一点。
到目前为止,我有一个跨度(使用引导程序,所以它是一个字形图标(X)) - 用户应该单击它来删除特定记录。问题是,当单击此按钮时,它将当前表的所有唯一 ID 发送到 ajax 脚本,这显然不好。
这是我所拥有的:生成的记录:
while($row = $ret2->fetchArray(SQLITE3_ASSOC) ){
echo '<tr>';
echo '<th>#</th>';
echo '<th>'.$row['type'].'</th>';
echo '<th>'.$row['func'].'</th>';
echo '<th>'.$row['trg'].'</th>';
echo '<th>
<span id="deleteRecordSpan" style="cursor:pointer" class="remove glyphicon glyphicon-remove">'.$row['uniqueID'].'</span>
</th>';
echo '</tr>';
$loop++;
}
到 ajax 脚本:
<script>
$(document).ready(function() {
$('.remove').click(function() {
var input = $('.remove').text();
$.ajax({ // create an AJAX call...
data: {
uniqueid: input,
},
type: 'POST', // GET or POST from the form
url: './ajax/deleteRecord.php', // the file to call from the form
success: function(response) { // on success..
show10();
allToday();
}
});
});
});
</script>
跨度的内容只有与其内部关联的唯一 ID。
我正在努力找到一种方法来仅将单击的特定 UniqueID 发送到 ajax 脚本以进行删除。
以下是正在发生的事情:
UniqueID | Type | Func | otherID |
500 | Computer | Full | 123 | X
557 | Computer | Full | 125 | X
654 | Computer | Full | 12564 | X
如果单击上表中的任何 X,它会500557654
发送到 ajax 脚本,当我需要它只发送与 X 关联的 ID 时。
当你使用选择器时$('.remove')
jQuery将选择每个元素与类remove
,在你的例子中,所有的remove
跨越。
将input = $('.remove').text()
更改为input = $(this).text()
。
此外,还可以将属性分配给span
并从中获取 ID:
在你的 PHP 中:
<span id="deleteRecordSpan" data-uid="'.$row['uniqueID'].'" style="cursor:pointer" class="remove glyphicon glyphicon-remove">'.$row['uniqueID'].'</span>
然后在你的 JavaScript 中:
var input = $(this).data('uid');
因此,您无需使用纯文本。