1. Get a single checkbox selected item (three writing methods):
$("input:checkbox:checked").val()
or
$("input:[type='checkbox']:checked").val();
or
$("input:[name='ck']:checked").val();
2. Get multiple checkbox selected items:
$('input:checkbox').each(function() {
If ($(this).attr('checked') ==true) {
alert($(this).val());
}
});
or
('input:checkbox').map(function () {
Return(this).val();
}).get().join(',') ;
3. Set the first checkbox as the selected value:
$('input:checkbox:first').attr("checked",'checked');
or
$('input:checkbox').eq(0).attr("checked",'true');
4. Set the last checkbox as the selected value:
$('input:radio:last').attr('checked', 'checked');
or
$('input:radio:last').attr('checked', 'true');
5. Set any checkbox as the selected value based on the index value:
$('input:checkbox).eq(index value).attr('checked', 'true');Index value=0,1,2....
or
$('input:radio').slice(1,2).attr('checked', 'true');
6. Select multiple checkboxes:
Select the 1st and 2nd checkboxes at the same time:
$('input:radio').slice(0,2).attr('checked','true');
7. Set the checkbox to the selected value based on the Value value:
$("input:checkbox[value='1']").attr('checked','true');
8. Delete the checkbox with Value=1:
$("input:checkbox[value='1']").remove();
9. Which checkbox to delete:
$("input:checkbox").eq(index value).remove(); index value=0,1,2....
For example, delete the third checkbox:
$("input:checkbox").eq(2).remove();
10. Traverse checkbox:
$('input:checkbox').each(function (index, domEle) {
//Write code
});
11. Select all
$('input:checkbox').each(function() {
$(this).attr('checked', true);
});
12. Deselect all:
$('input:checkbox').each(function () {
$(this).attr('checked',false);
});