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

时间:2016-07-12 10:14:54

标签: javascript php html ajax

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

^.*
//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;
}
?>

1 个答案:

答案 0 :(得分:0)

    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>