English 中文(简体)
jQuery Delete From Table
原标题:

This code does following:

  • Click on "Delete" in item row -> deletes item row
  • Click on "Delete" in category row -> deletes category row and all item rows (in all table)

Need it to do following:

  • Click on "Delete" in item row -> delete item row (WORKS PERFECTLY)
  • Click on "Delete" in category row -> delete category row and all items inside that category row (DOES WORK YET)

Live demo: http://usercp.athensluna.net:81/mall_manager2/

Table:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Go go Luna!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="styles.css" />
        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript" src="jquery.jeditable.js"></script>
        <script type="text/javascript">                                         
            $(document).ready(function() {
                $("tr.item .delete").click(function(){
                    $.post("do.php", { what: "delete", q: $(this).attr("id") });
                    $(this).parent().parent().fadeOut("fast");
                });

                $("tr.category a.delete").click(function(e){
                    $(this).parent().parent().fadeOut("fast");
                    $(this).parent().parent().nextAll("tr.item").fadeOut("fast");
                });

            });
        </script>

    </head>
    <body>
        <div id="wrapper">
            <div id="header"></div>
            <div id="main">
<table class="stats" width="100%">
    <tr class="category">
        <td colspan="2">Costumes<br /><small>Make your character look uniqe</small></td>
        <td width="80"><a href="#" class="delete" id="category:1">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:1:1">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:1:2">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:1:3">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:1:4">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:1:5">Delete</a><br />Invisible</td>
    </tr>
    <tr class="category">
        <td colspan="2">Costumes<br /><small>Make your character look uniqe</small></td>
        <td width="80"><a href="#" class="delete" id="category:2">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:2:1">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:2:2">Delete</a><br />Invisible</td>
    </tr>
    <tr class="category">
        <td colspan="2">Costumes<br /><small>Make your character look uniqe</small></td>
        <td width="80"><a href="#" class="delete" id="category:3">Delete</a><br />Invisible</td>
    </tr>
    <tr class="item">
        <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
        <td width="120">1000 Athen Coins<br />$1 Paypal</td>
        <td width="80"><a href="#" class="delete" id="item:3:1">Delete</a><br />Invisible</td>
    </tr>
</table>
            </div>
            <div id="footer">Template by <a href="http://danspd.com" target="_blank">DanSpd</a></div>
        </div>
    </body>
</html>
问题回答

Can you change the HTML at all?

The jQuery would be easier (and the HTML more descriptive) if the HTML used a <tbody> tag for each category, like this:

<table class="stats" width="100%">
    <tbody>
        <tr class="category">
            <th scope="rowgroup" colspan="2">Costumes<br /><small>Make your character look uniqe</small></th>
            <td width="80"><a href="#" class="delete" id="category:1">Delete</a><br />Invisible</td>
        </tr>
        <tr class="item">
            <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
            <td width="120">1000 Athen Coins<br />$1 Paypal</td>
            <td width="80"><a href="#" class="delete" id="item:1:1">Delete</a><br />Invisible</td>
        </tr>
        ...
    </tbody>
    <tbody>
        <tr class="category">
            <th scope="rowgroup" colspan="2">Costumes<br /><small>Make your character look uniqe</small></th>
            <td width="80"><a href="#" class="delete" id="category:2">Delete</a><br />Invisible</td>
        </tr>
        <tr class="item">
            <td>Godly Weapon<br /><small>Super wepons of gods</small><br /><small>images/godly_weapon.png</small></td>
            <td width="120">1000 Athen Coins<br />$1 Paypal</td>
            <td width="80"><a href="#" class="delete" id="item:2:1">Delete</a><br />Invisible</td>
        </tr>
        ...
    </tbody>
</table>

Then your jQuery could be re-written like this:

    <script type="text/javascript">                                         
            $(document).ready(function() {
                    $("tr.item .delete").click(function(){
                            $.post("do.php", { what: "delete", q: $(this).attr("id") });
                            $(this).parent().parent().fadeOut("fast");
                    });

                    $("tr.category a.delete").click(function(e){
                            $(this).parents( tbody ).fadeOut("fast");
                    });

            });
    </script>
$("tr.category a.delete").click(function(e){

  e.preventDefault();

  // find the row
  var $tr = $(this).parent("tr:first");

  // find the next row with class  category 
  var $nextcategory = $tr.next("tr.category");

  // select next  item  rows after next of the next  category  row
  var $nextitemrows = $nextcategory.next( tr.category ).nextAll( tr.item );

  // remove next  item  rows before next of the next  category  row
  $nextcategory.nextAll("tr.item").not( $nextitemrows ).remove();

  // remove next category
  $nextcategory.remove();
});


$("tr.item a.delete").click(function(e){

  e.preventDefault();

  // find the row & remove itself
  $(this).parent("tr:first").remove();
});

Don t know what jQuery version you are using, or if you are testing with IE, but there is a bug with jQuery 1.3.2 using fadeIn/fadeOut that might be causing problems:

http://dev.jquery.com/ticket/4440

Don t know if that s the problem, but thought I d mention it.





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签