0

这是有问题的代码:

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

            e.preventDefault();
            var parent = $(this).parent();

            $.ajax({
                type: 'get',
                url: 'deletemenu.php',
                data: 'ajax=1&delete=' + parent.attr('id').replace('menu-',''),
                beforeSend: function() {
                parent.animate({'backgroundColor':'#fb6c6c'},300);
            },
                    success: function() {

                    parent.slideUp(300,function() {
                        parent.remove();
                    });

                    $('.menusub').slideUp(300,function() {
                        $('.menusub').remove();
                    });
                }
            });
        });

好的,我想做的是向上滑动并删除特定菜单下的所有子菜单(删除此菜单时)。现在我有$('.menusub').slideUp(300,function() { $('.menusub').remove(); });。为达到这个。当然,这段代码的问题在于它会使所有子菜单(具有 menusub 类)消失。我如何确保只有特定菜单下的子菜单才会消失?

顺便说一下,这是查看菜单的代码

            $result = mysql_query("SELECT * FROM Menu") or die(mysql_error());
        while($row = mysql_fetch_array($result)){
            echo '<div class="menu" id="menu-',$row['menu_id'],'">';
            echo $row['menu_name']. " - ". $row['menu_weight']. "<a class='delete' href='?delete=".$row['menu_id']."'> delete</a>";
            echo '</div>';

            $sub = mysql_query("SELECT * FROM SubMenu INNER JOIN Menu WHERE SubMenu.menu_id = Menu.menu_id AND Menu.Menu_id = ".$row['menu_id']."") or die(mysql_error());
            while($subrow = mysql_fetch_array($sub)){
                echo '<div class="menusub" id="menusub-',$subrow['submenu_id'],'">';
                echo "-- ".$subrow['submenu_name']." - ".$subrow['submenu_weight']."<a class='deletesub' href='?deletesub=".$subrow['submenu_id']."'> delete</a><br/>";
                echo '</div>';
            }
        }`

Tnx 回复!

4

1 回答 1

0

这可能会帮助您:

改变 :

$('#menusub-').slideUp(300,function() {
    $('.menusub').remove();
});

至:

var rowid = parent.attr('id').split('-')[1];
$('#menusub-'+rowid).slideUp(300,function() {
    $(this).remove();
});

编辑: 建议,rel向子菜单添加一个属性,如下所示:

rel="$row['menu_id']"

并使您的脚本如下:

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

    e.preventDefault();
    var parent = $(this).parent();
    parent.slideUp(300,function() {
        parent.remove();
});

var id = parent.attr('id').split('-')[1];
$('div.submenu').each(function(index, element) {
    $this = $(this);
    if($this.attr('rel') == id) {
        $this.slideUp(300,function() {
            $this.remove();
        });
    }
    });

});
于 2013-04-02T18:13:20.790 回答