开发者

get value of checked[ALL] or unchecked box jquery

I have read this.

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 store开发者_StackOverflow中文版Check
    });

    $("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

I am trying to get the value of the checkboxs are checked as an array.

for example

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"


It's not clear wether you want to do it client-side or server-side. Anyway that's a JQuery snippet that fills an array with all the checked checkBoxes:

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


 var checkedCheckboxes = $(":checkbox:checked").toArray();


Try:

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;
         }
    }
}

And using the gollowing html code:

<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>

And so on...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜