gpt4 book ai didi

javascript - 使用 JQuery 和 PHP 删除表行

转载 作者:行者123 更新时间:2023-12-02 20:14:35 25 4
gpt4 key购买 nike

我对 PHP、Javascript 和 AJAX 很陌生,如果这是一个简单的问题,我很抱歉。我正在开发插件;在这个插件中,我正在用 PHP 开发一个表。用户可以添加指向特定小部件的链接以显示在其主页上。我正在尝试使用 Javascript 和 AJAX 创建一个函数,从列表和数据库中删除一行。我正在尝试为每个 TR 分配一个唯一的 ID,但我不确定如何执行此操作..这是我的代码:

<table id="links" width="350px" border="1">
<tr>
<td></td>
<td><b>Display</b></td>
<td><b>Link *</b></td>
<?php

if ( isset($_POST['links']) ) {

$displays = $_POST['displays'];
$links = $_POST['links'];
$domain = $_POST['domain'];

foreach($links as $i => $link) {
if ( empty($displays[$i]) )
$displays[$i] = "";
if( empty($links[$i]))
unset($links[$i]);

}


$autodomain = array('displays' => $displays, 'links' => $links, 'domain' => $domain);

$options = get_option("autodomain_links") or array();
$options[] = $autodomain;
//$dump = array();
update_option("autodomain_links", $options);

}
$options = get_option("autodomain_links");

if ($options) {

foreach($options as $option) if ($tmp++ < 15) {

$displays = $option['displays'];
$links = $option['links'];
$domain = $option['domain'];
foreach($links as $i => $link)
{
echo '<tr>';
echo '<td><a href="#" class="delete"/><img alt="" border="0" src="https://jarkin.its-express-tst.syr.edu/wp-content/uploads/2011/06/delete.png" /></a></td>';
echo '<td>' . $displays[$i] . '</td>';
echo '<td>' . $link . ' ' . $domain . '</td>';
echo '</tr>';
;

}

}

}


?>

</table>

这是 Javascript/AJAX:

<script type="text/javascript" >
$(document).ready(function()
{
$('table#links td a.delete'').click(function()
{
if (confirm("Are you sure you want to delete this row?"))
{
var id = $(this).parent().parent().attr('id');
var data = 'id=' + id ;
var parent = $(this).parent().parent();

$.ajax(
{
type: "POST",
url: "delete_row.php",
data: data,
cache: false,

success: function()
{
parent.fadeOut('slow', function() {$(this).remove();});
}
});
}
});
});
</script>

Javascript/ajax 使用表 id 和 TR id 作为父级来标识行 - 但由于我使用 PHP,我不知道如何为每个 TR 生成唯一的 ID。

最佳答案

您可以通过$i来定义它:

       foreach($links as $i => $link) 
{
echo '<tr id="id_'.$i.'">'; //<--here
echo '<td><a href="#" class="delete"/><img alt="" border="0" src="https://jarkin.its-express-tst.syr.edu/wp-content/uploads/2011/06/delete.png" /></a></td>';
echo '<td>' . $displays[$i] . '</td>';
echo '<td>' . $link . ' ' . $domain . '</td>';
echo '</tr>';
;

}

那么每行的 id 为 id_{rowNumber}

关于javascript - 使用 JQuery 和 PHP 删除表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6494727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com