单击另一个父级时隐藏父级的子菜单内容


Hiding the child menu content of the parent when another parent is clicked

当我尝试单击另一个父级时,我无法隐藏当前父级的子菜单。当单击另一个父菜单时,我需要隐藏与上一个父菜单对应的子菜单。目前,我的代码除了显示当前的父子项外,还显示以前的父子项。

//loads the children corresponding to the parent
<?php
include_once 'dbconfig.php'; 
$cat = array();
if(!empty($_POST))
{   
}
elseif(!empty($_GET)) {   
    if($_GET['feat']== 'getchild')
    { 
        if(!empty($_GET['c_id']))
        {
        $sql = "select a.category, a.category_id from cscart_category_descriptions a join cscart_categories b where a.category_id=b.        category_id and b.parent_id={$_GET['c_id']} and status='A'"; 
        $res = get_data_form_db($sql);
        
        foreach($res as $ob) 
        {
        $html.='<li><span id = "list'.$ob['category_id'].'"  onclick = "LinkClick('''.$ob['category_id'].''')" valu        e="'.$ob['category_id'].'">'.$ob['category'].'</span></li>';
        }
            
        if($html!='')
        {
        $html = "<ul>$html</ul>";
        }    
        echo $html;
        }    
        }    
        }
        else {
        echo "invalid type request";
        exit;
        }
function get_data_form_db($query)
{
        if(!empty($query))
        {
            //echo $query;
            $Result = mysql_query($query);
            //var_dump($Result);
            $i  = -1; 
            while($row = mysql_fetch_array($Result))
            {
                $rows[++$i] = $row;
            }
        }    
    return $rows;
}
?>
// ajax_index.php
// This file is used to load the parent from the database //and makes an ajax call to load its child.
<?php
include_once 'dbconfig.php'; 
?>
<html>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<body>
   <h1>Categories and sub Categories</h1>
   <?php
   $Result = mysql_query("select cd.category, cd.category_id from cscart_category_descriptions cd, cscart_categories cs where cd.category_id = cs.category_id and cs.parent_id = 0 and status ='A';");
    
    while($Row = mysql_fetch_array($Result))
    {
        $c_id[++$i] = $Row[1];
        $categories[$Row[1]] = $Row[0];
    }
    $c_id[0] = $i;
    
    for($i=0; $i < $c_id[0]; $i++)
    {
        echo '<li><span  onclick = "LinkClick('''.$c_id[$i].''')" >'.$categories[$c_id[$i]].'</span><span id = "list'.$c_id[$i].'"></span></li>';
        
    
    }
    
?>
   
</body>
<script type="text/javascript">
        var Clicked = {};
        var Previous = null;
        function LinkClick(id)
        {
            console.log(id);
            if(Clicked[id] == 1)
            {
                return false;
            }
            Clicked[id] = 1;
                        
            $.ajax({ 
                type:'GET',
                url: "test.php",
                data:{feat:"getchild", c_id:id},
                success:function(result){
                    console.log(result);
                    $("#list"+id).append(result);
                }
            });
            return true;
        }
        
        </script>
</html>
 

    You will need to modify some code like...
    <span id = "list'.$c_id[$i].'" class="submenu"></span>
   Also you will to modify js code like...
    <script type="text/javascript">
    var Clicked = {};
    var Previous = null;
    function LinkClick(id)
    {
        console.log(id);
        if(Clicked[id] == 1)
        {
            return false;
        }
        Clicked[id] = 1;
        $(".submenu").html('');            
        $.ajax({ 
            type:'GET',
            url: "test.php",
            data:{feat:"getchild", c_id:id},
            success:function(result){
                console.log(result);
                $("#list"+id).append(result);
            }
        });
        return true;
    }
    </script>