2014-11-04 188 views
0

有幾個像這樣的HTML片段。在「check_all」點擊時,我想檢查屬於這個「shopcart-con」類的「check_item」。但是當我點擊「check_all」時,所有的「check_item」都被選中了。如何解決它?由於如何讓特定父母的孩子?

HTML:

<div class="shopcart-con" data-merchant-id="1"> 
    <div class="shopcart-list">  
      <div class="shopcart-lstop"><input type="checkbox" class="check_all" name="select"/>fruit shop A</div> 
      <div class="shopcart-lscon"> 
      <ul class="shop_contentul" data-product-id="1"> 
       <li class="checkinp"><input type="checkbox" class="check_item" name="aihao" /></li> 
       <li class="checkimg"><img src="images/home10.png" width="100%" height="75px"></li> 
       <li class="checknum" > 
        <p class="lineheight30">apple</p> 
         <span class="shopcart-num"> 
         <input type="button" name="" class="inp-reduce"> 
         <input type="text" name="" class="num" value="5"> 
         <input type="button" name="" class="inp-add"> 
         </span> 
       </li> 
        ¥<span class="unit_price">8.00</span> 
        <p><font color="#808080" size="2px"><s> $12.00</s></font></p> 
        <p>x<label class="shuli">5</label></p> 
        <input class="delbtn" name=""> 
      </ul> 
      <div class="clear"></div> 
      </div> 
      <div class="shopcart-lscon"> 
      <ul class="shop_contentul" data-product-id="2"> 
       <li class="checkinp"><input type="checkbox" class="check_item" name="aihao" /></li> 
       <li class="checkimg"><img src="images/home10.png" width="100%" height="75px"></li> 
       <li class="checknum" > 
        <span class="shopcart-num"> 
         <input type="button" name="" class="inp-reduce"> 
         <input type="text" name="" class="num" value="5"> 
         <input type="button" name="" class="inp-add"> 
         </span> 
       </li> 
        ¥<span class="unit_price">8.00</span> 
        <!-- <p><font color="#808080" size="2px"><s> $12.00</s></font></p> --> 
        <p>x<label class="shuli">5</label></p> 
        <input class="delbtn" name=""> 
      </ul> 
      <div class="clear"></div> 
      </div> 
      <div> 
       <p class="shopcart-money">共計<label class="product_quantity" data-merchant-id="1">10</label> sum:$<label class="price_sum" data-merchant-id="1" style="color:red">80</label></p> 
      </div> 
</div> 
</div> 

JS:

$('.check_all').click(function(){ 
     var checked_items = $('.check_all').closest('.shopcart-con').find('.check_item') 
     console.log(checked_items.length) 
     if($(this).prop('checked')){ 
      $.each(checked_items, function(i,val){ 
       val.checked = true; 
      }); 
     }else{ 
      $.each(checked_items, function(i,val){ 
       val.checked = false; 
      }); 
     } 
    }); 

回答

1

試試這個:您使用本$('.check_all').closest下找到所有check_allshopcart-con。但是,你需要找到shopcart-con的點擊check_all,因此使用$(this)$(this).closest

$('.check_all').click(function(){ 
     var checked_items = $(this).closest('.shopcart-con').find('.check_item') 
     console.log(checked_items.length) 
     if($(this).prop('checked')){ 
      $.each(checked_items, function(i,val){ 
       val.checked = true; 
      }); 
     }else{ 
      $.each(checked_items, function(i,val){ 
       val.checked = false; 
      }); 
     } 
    }); 

DEMO

你可以讓你的代碼更短,您不必重複所有的複選框,並設置其屬性,請參閱下面的代碼

$('.check_all').click(function(){ 
     var checked_items = $(this).closest('.shopcart-con').find('.check_item') 
     console.log(checked_items.length) 
     $(checked_items).prop('checked',this.checked); 
    }); 

DEMO

+0

它的工作原理。謝謝 – user1179442 2014-11-04 06:42:44

+0

高興地幫助你:) – 2014-11-04 06:44:54

相關問題