0

首先,我不知道如何指定标题抱歉。

我有一个非常混乱的代码,但基本上这就是它应该做的。当按下删除按钮时,整个 tr 应该消失。目前只有最后一个带有按钮的 td 会消失。我尝试替换 a at

$('a').click(function(){

与 tr。但是当我这样做时,该行将消失,但不会在数据库中删除。有谁知道解决方案?提前致谢

ps:对不起我的英语和显示不好的编码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="style.css" />
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $('document').ready(function(){
        $('a').click(function(){
        var del_id = $(this).attr('id');
        var parent = $(this).parent();
        $.post('delete.php', {id:del_id},function(data){
        parent.slideUp('slow', function() {$(this).remove();});
        });
        });
        });
    </script>
</head>
<body>
    <div id="content1"></div>
    <table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
        <thead>
            <tr>
                <th class="nosort"><h3>ID</h3></th>
                <th><h3>Merk</h3></th>
                <th><h3>Type</h3></th>
                <th><h3>Imei</h3></th>
                <th><h3>Serienummer</h3></th>
                <th><h3>Kleur</h3></th>
                <th><h3>Locatie</h3></th>
            </tr>
        </thead>

        <tbody>
            <?php
                include 'Includes/database_connection.php';
                $sql = "select *
                        FROM units";
                $result = mysql_query($sql,$con);       
                while($row = mysql_fetch_assoc($result)){
                echo "<tr>";
                echo "<td>{$row['id']}</td><td>{$row['merk']}{$row['type']}{$row['imei']}{$row['serienr']}{$row['kleur']}{$row['locatie']}<a href=\"javascript:return(0);\" id=\"{$row['id']}\"><img src=\"images\delete_icon.gif\" height=\"12px\" width=\"12px\"/></a></td>";
                echo "</tr>";
                }       
                                while($row = mysql_fetch_array($result)) {

                                echo "<tr>";
                                echo "<td>".$row['id']."</td>";
                                echo "<td>".$row['merk']."</td>";
                                echo "<td>".$row['type']."</td>";
                                echo "<td>".$row['imei']."</td>";
                                echo "<td>".$row['serienr']."</td>";
                                echo "<td>".$row['kleur']."</td>";
                                echo "<td>".$row['locatie']."</td>";
                                ?>
                                <td><a href="#" onclick="window.open('test1.php?id=<?php echo $row['id']; ?>', 'newwindow', 'width=400, height=600'); return false;"><img src="images/gtk-edit.png"/></a></td>

                                <?php

                                echo "</tr>";
                            }


                            ?>
                        </tbody>
                  </table>
        <div id="controls">
            <div id="perpage">
                <select onchange="sorter.size(this.value)">
                <option value="5" selected="selected">5</option>
                    <option value="10" >10</option>
                    <option value="20">20</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                </select>
                <span>Weergeven per pagina</span>
            </div>
            <div id="navigation">
                <img src="images/first.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1,true)" />
                <img src="images/previous.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1)" />
                <img src="images/next.gif" width="16" height="16" alt="First Page" onclick="sorter.move(1)" />
                <img src="images/last.gif" width="16" height="16" alt="Last Page" onclick="sorter.move(1,true)" />
            </div>
            <div id="text">Pagina <span id="currentpage"></span> van <span id="pagelimit"></span></div>
        </div>
        <script type="text/javascript" src="script.js"></script>
        <script type="text/javascript">
      var sorter = new TINY.table.sorter("sorter");
        sorter.head = "head";
        sorter.asc = "asc";
        sorter.desc = "desc";
        sorter.even = "evenrow";
        sorter.odd = "oddrow";
        sorter.evensel = "evenselected";
        sorter.oddsel = "oddselected";
        sorter.paginate = true;
        sorter.currentid = "currentpage";
        sorter.limitid = "pagelimit";
        sorter.init("table",1);
      </script>
    </div>
</body>

还有我的流程文件

<?php
include 'Includes/database_connection.php';
$id = $_POST['id'];
$safeid = mysql_real_escape_string($id);
$query = mysql_query("delete from units where id=$id", $con);
?>
4

1 回答 1

0

当您找到 的父级时a,您正在找到td. 这可以解释为什么您看到td消失而不是tr.

出于这个原因,而不是parent()使用closest("tr")来查找行:

//var parent = $(this).parent();
var parent = $(this).closest("tr");

然后 slideUp/remove parent... 然后您可能还想重命名parentparentRow.

至于删除数据库中的数据。看来您的代码正在尝试查找 的id属性a,因此您需要确保其中的id属性a与您要从数据库中删除的 id 匹配...类似于<a href="#" id="<?php echo $row['id']; ?>" ... />

另一种解决方案,如果您希望能够单击行中的任意位置(因为您提到尝试过),您可以这样做:

    $('document').ready(function () {
        $('tr').click(function () {
            //this assumes id will always be within the first td
            //you could also put classes on your td's to identify
            var del_id = $(this).find("td").first().text();
            var row = $(this);
            $.post('delete.php', {
                id: del_id
            }, function (data) {
                row.slideUp('slow', function () {
                    row.remove();
                });
            });
        });
    });
于 2013-04-26T15:06:28.490 回答