Solution to select all and get value value value of the iCheck plug-in

Keywords: JQuery Google

When we use the jQuery iCheck plug-in, we encounter a problem, that is, when we use the normal js select all function, it is invalid.

$("#checkall").click(
        function(){
            if(this.checked){
                $("input[name='checkname']").each(function(){this.checked=true;});
            }else{
                $("input[name='checkname']").each(function(){this.checked=false;});
            }
        }
    );

This is OK for the default checkbox box, but it will not work when the iCheck plug-in is used.

So how to solve it?

Finally, the solution is found in stackoverflow:

Here is the address: http://stackoverflow.com/questions/17820080/function-select-all-and-icheck

//Select all to get the value
	var checkAll = $('input.all');
	var checkboxes = $('input.check');
	checkAll.on('ifChecked ifUnchecked', function(event) {
		if (event.type == 'ifChecked') {
			checkboxes.iCheck('check');
		} else {
			checkboxes.iCheck('uncheck');
		}
	});
	checkboxes.on('ifChanged', function(event){
		if(checkboxes.filter(':checked').length == checkboxes.length) {
			checkAll.prop('checked', 'checked');
		} else {
			checkAll.removeProp('checked');
		}
		checkAll.iCheck('update');
	});

After the problem of all selection is solved, a new problem is encountered. To get the value of the selected checkbox, use $(this).attr('checked '); if you can't get the value ~, the egg hurts.

Finally, after several Google searches, we find the inspiration in stack overflow to judge the Boolean value of checkbox, using $(this).is(':checked');

The final code solution is as follows:

	$(".ajax-delete").click(function(){
	var url = $(this).attr('data-url');
	var str="";
	var ids="";
	$("input[name='id']:checkbox").each(function(){
		if(true == $(this).is(':checked')){
			str+=$(this).val()+",";
		}
	});
	if(str.substr(str.length-1)== ','){
		ids = str.substr(0,str.length-1);
	}
	console.log(ids);
});

end.

Posted by showman on Mon, 02 Dec 2019 05:49:54 -0800