Jquery already get checked checkbox

A, Jquery several ways to obtain the checkbox has been selected

1. take a look at how native js acquired, first of all get checkbox name for a target object, received at this time is an array, which is stored in an array of all, and then traverse the array of all, if the current click the object, add an empty array to define.

var objetct = document.getElementsByName(“su_check”);
var check_list = [];
for (k in object) {
if (object[k].checked)
check_list.push(object[k].value);
}

2. The method of less friendly browser compatible ie, recommended for replacement into the following loop: for (var i = 0; i <object.length; i ++) 

3. jquery get've clicked checkbox checkbox

function test(){
    var chk_list =[]; 
    $('input[name="sw_test"]:checked').each(function(){ 
        chk_list.push($(this).val()); 
    }); 
    Alert (chk_list.length == 0 ? ' You have not select anything! ' : chk_list);
}

 

Guess you like

Origin www.cnblogs.com/starwei/p/12459934.html