单击选择全部时获取复选框的值

时间:2014-06-13 10:49:41

标签: javascript html

_____ _____________
name | select all o
----- -------------
abcd |     o
efcg |     o
qwer |     o
zxcv |     o
-------------------

当我单击全选复选框时,它应该选中所有复选框,相应的所有值应该使用javascript存储在像[abcd,efch,qwer,zxcv]这样的数组[]中。 提前致谢

2 个答案:

答案 0 :(得分:0)

下面的函数应该由'Select All'复选框的'onchange'事件调用

例如:

  

<input type="checkbox" name="select_all" value="select_all" onchange="checkAll()">   

function checkAll() {

    var checkboxes = document.getElementsByClassName('class_name_of_all_checkboxes'); // You have to give this class name to all checkboxes you want to check

    var checkbox_values = []; // If you need to access this array outside this function, then you will have to define it before the function

    for (index = 0; index < checkboxes.length; ++index) {
        if(!checkboxes[index].checked) {
            checkboxes[index].checked = true;
            checkbox_values.push(checkboxes[index].value);
        }
    }
}

答案 1 :(得分:-1)

选中所有代码复选框

function checkAll(ckbAll) {

      var a[] = new array[50];
                    if (ckbAll.checked == true) {
                        $('input[type=checkbox]').each(function() {
                            $(this).attr('checked', 'checked');
                             a=$(this).val();
                        });
                    } else {
                        $('input[type=checkbox]').each(function() {
                            $(this).removeAttr('checked');
                             //do your logic to delete it from array
                        });
                    }
                }