Home >Web Front-end >JS Tutorial >Summary of 12 tips for operating checkboxes in jquery_jquery

Summary of 12 tips for operating checkboxes in jquery_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:01:231015browse

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() {
                                                                                 ) ==true) {
                                                                                                   

3. Set the first checkbox to 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 according to 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 and 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 according to 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.... To 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() {                                                                                                               >});

12. Unselect all

$('input:checkbox').each(function () {

                                                                              🎜>});

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn