2012-09-10 15 views
0

我有更多的複選框,輸入調用函數「boxclick」當這個用戶點擊:jQuery的檢查,並取消所有動作

<input id="1box" type="checkbox" name="selected[]" onclick="boxclick(this,'1')"> 
<input id="2box" type="checkbox" name="selected[]" onclick="boxclick(this,'2')"> 
<input id="3box" type="checkbox" name="selected[]" onclick="boxclick(this,'3')"> 
<input id="4box" type="checkbox" name="selected[]" onclick="boxclick(this,'4')"> 

對於選擇/取消所有我用簡單的jQuery腳本:

<input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" checked="checked"> 

我的問題是當我檢查/取消選中所有功能「boxclick」不運行。

// == a checkbox has been clicked == 
    function boxclick(box,category) { 
    if (box.checked) { 
     show(category); 
    } else { 
     hide(category); 
    } 
    } 
+0

你能告訴我breif ...?我想你要檢查所有的一個複選框被選中......?對。? – Gautam3164

+0

你能告訴我們'boxclick'的功能是什麼樣子嗎? –

+0

我已更新我的問題與boxclick代碼 –

回答

2

將複選框的屬性設置爲勾選不會自動爲您假冒點擊事件!由於您在某些「onclick」處理程序中放置了這些boxclick函數調用,因此您必須觸發一個虛假的單擊事件才能讓您的javascript認爲它在所有框中都點擊了點擊了。爲了觸發虛假點擊的事件使用$(「#不管」)的觸發器(「點擊」),例如:

$('input[name*=\'selected\']').trigger('click'); 

如果你想查看/取消所有功能,你必須說「OK,主複選框,請如果發生這種情況,請將我的奴隸盒(您的其他盒子在頁面上)的檢查屬性設置爲匹配主人的當前選中的屬性。就像這樣:

function checkAllorUncheckAll(event){ 

    var chiefCheckbx = event.currentTarget; 
    var myBoxes = $('input[name*=\'selected\']'); 

    myBoxes.prop("checked", $(chiefCheckbx).prop("checked")).trigger('click'); 

} 

$('#masterCheckBox').on('change',checkAllorUncheckAll); 
+0

Thk爲答案,默認我的複選框都檢查,如何解決? –

+0

你想取消選擇/檢查所有與我看到的一個主複選框,所以請看看我的重做答案:) – sajawikio

2

我認爲你可以這樣做:

HTML

<input id="1box" type="checkbox" name="selected[]"> 
<input id="2box" type="checkbox" name="selected[]"> 
<input id="3box" type="checkbox" name="selected[]"> 
<input id="4box" type="checkbox" name="selected[]"> 

jQuery的

如果你想檢查/取消所有checkbox

var checkboxes = $('input[type=checkbox][name^=selected]'); 
checkboxes.on('click', function() { 
    checkboxes.prop('checked', this.checked); 
}); 

如果你想選擇在任何一個時間:

var checkboxes = $('input[type=checkbox][name^=selected]'); 
checkboxes.on('click', function() { 
    checkboxes.prop('checked', false); 
    this.checked = !this.checked; 
});​ 

DEMO

+0

這將檢查所有複選框,只要你點擊任何一個單一的,並永遠不會允許你取消選中它們,既不其中似乎是他們瞄準的功能。 –

0

我猜

$('input[name*=\'selected\']').attr('checked', this.checked).trigger('click');

應該在這裏做的伎倆

+0

如果OP的boxclick函數已經單獨檢查每個框,那麼attr('checked',this.checked)鏈應該不存在。 OP沒有發佈boxclick函數代碼,所以我們目前還不能確定,儘管如果OP想要的話,如果OP的boxClick函數不檢查/取消選中,那麼執行.attr('checked',this.checked)已經切換個別的盒子(但如果它是一個切換,OP將需要注意 - boxClick代碼可能與OP真的想要做什麼衝突! – sajawikio

+0

@sajawikio他說,函數boxClick沒有被調用設置attr檢查。它不會是因爲它從來沒有實際點擊。所以我告訴他觸發點擊事件後設置attr檢查或取消選中。 –

0

演示 http://jsfiddle.net/H37cb/

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" /></script> 

<script type="text/javascript"> 
$(document).ready(function(){ 

$('input[name="all"],input[name="title"]').bind('click', function(){ 
var status = $(this).is(':checked'); 
$('input[type="checkbox"]', $(this).parent('li')).attr('checked', status); 
}); 

}); 
</script> 




<div id="wrapper"> 
<li style="margin-top: 20px"> 
    <input type="checkbox" name="all" id="all" /> <label for='all'>All</label> 
    <ul> 
    <li><input type="checkbox" name="title" id="title_1" /> <label for="title_1"><strong>Title 01</strong></label> 
    <ul> 
    <li><input type="checkbox" name="selected[]" id="box_1" value="1" /> <label for="box_1">Sub Title 01</label></li> 
    <li><input type="checkbox" name="selected[]" id="box_2" value="2" /> <label for="box_2">Sub Title 02</label></li> 
    <li><input type="checkbox" name="selected[]" id="box_3" value="3" /> <label for="box_3">Sub Title 03</label></li> 
    <li><input type="checkbox" name="selected[]" id="box_4" value="4" /> <label for="box_4">Sub Title 04</label></li> 
    </ul> 
    </li> 
    </ul> 
    <ul> 
    <li><input type="checkbox" name="title" id="title_2" /> <label for="title_2"><strong>Title 02</strong></label> 
    <ul> 
    <li><input type="checkbox" name="selected[]" id="box_5" value="5" /> <label for="box_5">Sub Title 05</label></li> 
    <li><input type="checkbox" name="selected[]" id="box_6" value="6" /> <label for="box_6">Sub Title 06</label></li> 
    <li><input type="checkbox" name="selected[]" id="box_7" value="7" /> <label for="box_7">Sub Title 07</label></li> 
    </ul> 
    </li> 
    </ul> 
</li> 
</div> 
+0

這個答案可能會更有利於OP和其他誰看到它後來,如果你解釋_how_它解決了這個問題,即使代碼是準確的,我們通常也希望詳細解釋代碼塊。 – Herbert

相關問題