2009-10-06 72 views
0

我是jQuery的noob ...我希望我已经解释得很清楚了;我有一个<ul>标题,当我使用$.post将一个条目添加到动态创建的列表时出现。添加的每个条目都有一个与其关联的删除/编辑按钮。当所有条目被删除时,jQuery隐藏ul标题

标题是这样的:

<ul class="addedItems">  
    <li>Month</li> 
    <li>Year</li> 
    <li>Cottage</li> 
    <li><span class="edit">edit</span></li> 
    <li><span class="del">delete</span></li> 
</ul> 


This all looks like this: 

Month  Year  Cottage <--this appears after I've added an entry 
--------------------------------  and I want it to stick around unless 
             all items are deleted. 


Dec   1990  Fir  edit/delete <--entries 
Jan   2000  Willow  edit/delete 

我的问题是:是否有某种条件,我可以用jQuery使用隐藏class="header"如果创建的

<ul class="header"> 
    <li>Month</li> 
    <li>Year</li> 
    <li>Cottage</li> 
</ul> 

我的动态列表所有的项目都被删除?我已经阅读了条件语句,如isnot与jq,但我并不真正了解他们的工作方式。 class="addedItems"中的所有项目都存储在由JSON生成的data中。

这是删除功能:

$(".del").live("click", function(){ 
     var del = this; 
     var thisVal = $(del).val(); 
     $.post("delete.php", { dirID : thisVal }, 
     function(data){ 
     if(confirm("Are you sure you want to DELETE this entry?") == true) { 
      if(data.success) { 
      //hide the class="header" here somwhere?? 
      $(del).parents(".addedItems").hide(); 
      } else if(data.error) { 
      // throw error if item does not delete 
      } 
     } 
     }, "json"); 
     return false; 
    }); //end of .del function 

这里是delete.php

<?php 

    if($_POST) { 


     $data['delID'] = $_POST['dirID']; 

     $query = "DELETE from //tablename WHERE dirID = '{$data['delID']}' LIMIT 1"; 

     $result = $db->query($query); 

     if($result) { 
     $data['success'] = true; 
     $data['message'] = "Entry was successfully removed."; 
     } else { 
     $data['error'] = true; 
     $data['message'] = "Item could not be deleted."; 
     } 

     echo json_encode($data); 
    } 

    ?> 

回答

0

我猜你需要的每一个功能:一旦叫

$(del).parents('.header').each(function(){ 
    if ($(this).children('.addedItems:visible').length == 0) 
    { 
    $(this).hide(); 
    } 
}); 

这个小小的脚本将隐藏没有任何孩子的.header的任何UL。

地方这个脚本您$(del).parents(".addedItems").hide();

+0

@Ghommey无法让它工作...... grr。 – Scott 2009-10-06 09:25:09

+0

男人...仍然没有运气。你可以在“live”点击功能中加入“each”吗?应该不重要吗? – Scott 2009-10-06 09:37:18

+0

试试这个。你得到一个JavaScript错误? – jantimon 2009-10-06 10:02:13

0

后试试这个:

if(confirm("Are you sure you want to DELETE this entry?") == true) { 
    if(data.success) { 
     //hide the class="header" here somwhere?? 
     $(del).parents(".addedItems").hide(); 

     //CHECK IF THERE ARE NOT ANY LI ITEMS IN UL 
     if($("ul.addedItems li").length==0){ 
      $("ul.header").hide();//HIDE HEADER LIST 
     } 

    } else if(data.error) { 
     // throw error if item does not delete 
    } 
} 
+0

你真的添加jQuery的PHP代码? – jantimon 2009-10-06 09:17:24

+0

是的..没有真正意识到你可以这样做..嗯。 – Scott 2009-10-06 09:25:54

+0

是的,你是对的@Ghommey,我是一个傻瓜:( – TheVillageIdiot 2009-10-06 09:33:55

0

基于这里的一些答案,这就是我想出了:

if($("ul.header").siblings("ul.addedItems").is(":visible")) { 
    } else { 
     $("ul.header").hide(); 
    } 

感谢大家为了洞察。

+0

就像一个魅力! – Scott 2009-10-06 10:09:25