代码之家  ›  专栏  ›  技术社区  ›  tree em

获取选中[全部]或未选中框jquery的值

  •  0
  • tree em  · 技术社区  · 15 年前

    我读过这个。

    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

    我试图得到复选框的值作为数组进行检查。

    例如

    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 回复  |  直到 15 年前
        1
  •  3
  •   mamoo    15 年前

    目前还不清楚您是想在客户端还是服务器端执行此操作。无论如何,这是一个jquery片段,它用所有选中的复选框填充数组:

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

    尝试:

    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>
    

    等等…