Javascript从数据库中删除/淡化表行

时间:2013-04-26 14:53:36

标签: javascript

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

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

中的a
$('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);
?>

1 个答案:

答案 0 :(得分:0)

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

因此,请使用parent()代替closest("tr")而不是//var parent = $(this).parent(); var parent = $(this).closest("tr");

parent

然后slideUp / remove parent ...然后您可能还想将parentRow重命名为id

删除数据库中的数据。您的代码似乎正在尝试查找a的{​​{1}}属性,因此您需要确保id中的a属性与<a href="#" id="<?php echo $row['id']; ?>" ... />匹配id你想要从db中删除...类似于 $('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(); }); }); }); });

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

{{1}}