2010-04-01 56 views
0

我已閱讀此內容。獲取選中[ALL]或未選中框jquery的值

Jquery checkbox

<input type="checkbox" name="checkGroup" id="all"> 
<input type="checkbox" name="checkGroup" id="one" value="1"> 
<input type="checkbox" name="checkGroup" id="two" value="2"> 
<input type="checkbox" name="checkGroup" id="three" value="3"> 
<input type="hidden" name="storeCheck" value=""> 


$(function(){ 
    $("#all").click(function(){ 
     $("input:checkbox[name='checkGroup']").attr("checked",$(this).attr("checked")); 
     //array[1,2,3] will be pass to value of storeCheck 
    }); 

    $("input:checkbox[name='checkGroup']:not('#all')").click (function(){ 
     var totalCheckboxes = $("input:checkbox[name='checkGroup']:not('#all')").length; 
     var checkedCheckboxes = $("input:checkbox[name='checkGroup']:not('#all'):checked").length; 

     if (totalCheckboxes === checkedCheckboxes) 
     { 
      $("#all").attr("checked" , true); 
     } 
     else 
     { 
      $("#all").attr("checked" , false); 
     } 
    }); 
}); 

Demo

我試圖讓checkboxs的價值進行檢查數組。

例如

if I checked All 
    Get value array_check = 1,2,3 and passed this array to hidden name="storeCheck" 
otherwise: 
    Get value of array_check(checkboxs checked).and passed this array to hidden name="storeCheck" 

回答

3

無論您想這樣做,客戶端或服務器端目前尚不清楚。反正就是這樣充滿了所有選中的複選框數組jQuery的片段:

var checkedCheckboxes = new Array(); 
$('input:checkbox[name="checkGroup"]').each(function(index) { 
    if ($(this).attr('checked') == 'true') { checkedCeckboxes.push(this);} 
}); 
1
var checkedCheckboxes = $(":checkbox:checked").toArray(); 
0

嘗試:

function getChecked() 
{ 
    var array; 
    var addition; 
    var count = 0; 
    for (var k = 1; k < document.getElementById('boxes').elements.length; k++) 
    { 
     if (document.getElementById('boxes').elements[k].checked == true) 
     { 
      addition = k + ''; 
      array = array + '_' + addition; 
      count = count + 1; 
     } 
    } 
} 

並採用gollowing的html代碼:

<form id='boxes' name='boxes'> 
    <input name='list' type='checkbox' id='checkbox' value='1'> 
    <input name='list' type='checkbox' id='checkbox' value='2'> 
    <input name='list' type='checkbox' id='checkbox' value='3'> 
    <input name='list' type='checkbox' id='checkbox' value='4'> 
</form> 

等等上...

+0

數組,加法和計數變量只是我自己的代碼中的東西,你可以刪除它,如果需要的話。 – lugte098 2010-04-01 09:05:51