2013-05-17 21 views
0

我有多個複選框的功能..如果我們檢查多個孩子需要檢查,反之亦然。它只發生在第三次點擊。 如果我們點擊allcheck所有的孩子被檢查,反之亦然..但我需要的功能是,如果即使一個孩子被檢查,如果我們點擊allcheck剩下的需要檢查..如果我們取消選中,即使一個孩子allcheck需要unchecked.I使用該代碼我們可以在同一個點擊功能中同時使用類名和輸入類型名稱

HTML:

<li class="inputradio"><input name="allCheck" type="checkbox" ></li> 

<li id="childcheckbox"><input name="childCheck" type="checkbox"></li> 

所有檢查

<div id="actions"><div id="box"> 
<ul><li class="inputradio"><input name="allCheck" type="checkbox" ></li> 
<li class="multiple"><a href="#" class="bt btleft">Multiple</a></li> 
<!--<li class="deletebutton"><a href="#" class="bt btleft">Delete</a></li> 
<li class="copy"><a href="#" class="bt btright">Copy</a></li>--> 
<li class="shatebutton"><a href="#" class="bt stmiddle">Share</a> 
<div id="smenu"> 

</div> 

孩子:

<div id="rightoutputimgae"> 
<div id="rightimgId" class="rightimg" rel="tooltip" 
content="<img src='jqe13/image/1.jpg' class='tooltip-image'/> "> 
<div id="outputimageId" class="outputimage"> 
<img src="jqe13/image/1.jpg" alt="Right Bottom Image"></div> 
</div> 
<ul><li id="childcheckbox"><input name="childCheck" type="checkbox"></li> 
<li id="outedit"><a href="#"><img src="jqe13/image/edit_s.PNG" alt="edit" title="Edit"> 
</a></li> 
<li id="outdelete"> 
<a href="#" onclick="deleteImg()"> 
<img src="jqe13/image/delet_c.PNG" alt="delete" title="Delete"></a></li> 
    <li id="outfullscreen"> 
    <a href="#"><img src="jqe13/image/fullscreen_c.PNG" alt="Full Screen" 
class="fullscreen" title="Full Screen"></a></li> 

的jQuery:

$('.inputradio').click(function() { 
    $('input[name=allCheck]').click(function() { 
     $("input[name='childCheck']").prop('checked', this.checked); 
    }); 

    $("input[name='childCheck']").click(function() { 
     if ($('input[name=childCheck]:checked').length === $('input[name=childCheck]').length) $('input[name=allCheck]').prop("checked", true); 
     else $('input[name=allCheck]').prop("checked", false); 
    }); 
}); 

誰能告訴我什麼是錯在這

+0

對不起。檢查編輯後的代碼 – madhu

+1

不要在其他點擊處理程序中分配點擊處理程序 - 每次單擊「.inputradio」時,都會向輸入添加其他點擊處理程序。 – nnnnnn

+0

括號'[]'中的所有內容都應該在等號後加引號。 '[name = childCheck]'應該是'name =「childCheck」]' –

回答

1
$('input[name=allCheck]').click(function() { 
    $("input[name='childCheck']").prop('checked', this.checked); 
}); 

$("input[name='childCheck']").click(function() { 
    if ($('input[name=childCheck]:checked').length === $('input[name=childCheck]').length) $('input[name=allCheck]').prop("checked", true); 
    else $('input[name=allCheck]').prop("checked", false); 
}); 

試試這個

0

應該

var allchk = $('input[name="allCheck"]').click(function(){ 
    chks.prop('checked', this.checked); 
}) 

var chks = $("input[name='childCheck']").click(function() { 
    allchk.prop("checked", chks.filter(':checked').length === chks.length); 
}); 
+0

它不工作.. :( – madhu

+0

@madhu結帳這個樣品http://jsfiddle.net/arunpjohny/HZEqB/1/ –

+0

它的工作在這裏..如果我整合在我的代碼..它不工作 – madhu

0

試試這個

$('.inputradio').click(function() { 

    if ($('.inputradio input').is(':checked')) { 
    //do something 
    } 
    if($('#childcheckbox input').is(':checked')) { 
    //do smoething else 
    } 
}); 
相關問題