Home >Web Front-end >JS Tutorial >Summary of JQuery CheckBox (checkbox) operation methods_jquery

Summary of JQuery CheckBox (checkbox) operation methods_jquery

WBOY
WBOYOriginal
2016-05-16 16:03:591334browse

1. Get a single checkbox selected item (three writing methods):

Copy code The code is as follows:

$("input:checkbox:checked").val()

or
Copy code The code is as follows:

$("input:[type='checkbox']:checked").val();

or
Copy code The code is as follows:

$("input:[name='ck']:checked").val();

2. Get multiple checkbox selected items:
Copy code The code is as follows:

$('input:checkbox').each(function() {
If ($(this).attr('checked') ==true) {
alert($(this).val());
            }
});

or
Copy code The code is as follows:

('input:checkbox').map(function () {                                           Return(this).val();
}).get().join(',') ;


3. Set the first checkbox as the selected value:
Copy code The code is as follows:
$('input:checkbox:first').attr("checked",'checked');

or

Copy code The code is as follows:
$('input:checkbox').eq(0).attr("checked",'true');


4. Set the last checkbox as the selected value:
Copy code The code is as follows:
$('input:radio:last').attr('checked', 'checked');

or

Copy code The code is as follows:
$('input:radio:last').attr('checked', 'true');


5. Set any checkbox as the selected value based on the index value:
Copy code The code is as follows:
$('input:checkbox).eq(index value).attr('checked', 'true');Index value=0,1,2....

or

Copy code The code is as follows:
$('input:radio').slice(1,2).attr('checked', 'true');


6. Select multiple checkboxes:
Select the 1st and 2nd checkboxes at the same time:

Copy code The code is as follows:
$('input:radio').slice(0,2).attr('checked','true');


7. Set the checkbox to the selected value based on the Value value:
Copy code The code is as follows:

$("input:checkbox[value='1']").attr('checked','true');

8. Delete the checkbox with Value=1:
Copy code The code is as follows:

$("input:checkbox[value='1']").remove();

9. Which checkbox to delete:
Copy code The code is as follows:

$("input:checkbox").eq(index value).remove(); index value=0,1,2....

For example, delete the third checkbox:
Copy code The code is as follows:

$("input:checkbox").eq(2).remove();

10. Traverse checkbox:
Copy code The code is as follows:

$('input:checkbox').each(function (index, domEle) {
//Write code
});

11. Select all
Copy code The code is as follows:

$('input:checkbox').each(function() {
$(this).attr('checked', true);
});

12. Deselect all:
Copy code The code is as follows:

$('input:checkbox').each(function () {
$(this).attr('checked',false);
});

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