Summary of 12 tips for working with the jquery checkbox of checkbox

  • 2020-03-30 01:35:32
  • OfStack

1. Get a single checkbox check (three ways to write it)
$(" input: the checkbox checked "). The val ()
or
$(" input: [type = 'checkbox'] : checked "). The val ();
or
$(" input: [name = 'ck'] : checked "). The val ();

Get multiple checkbox checks
$(' input: the checkbox). Each (function () {
              If ($(this). Attr (" checked ") = = true) {
                              Alert ($(this). Val ());
              }
});

3. Set the first checkbox to the selected value
$(' input: the checkbox: first). Attr (" checked ", "checked");
or
$(' input: the checkbox). Eq (0). The attr (" checked ", "true");

4. Set the last checkbox to the selected value
$(' input: radio: the last). Attr (' checked ', 'checked');
or
$(' input: radio: the last). Attr (' checked ', 'true');

Set any checkbox to the selected value based on the index value
$(' input: the checkbox). Eq (index). Attr (' checked ', 'true'); Index = 0,...
or
$(' input: radio). Slice (1, 2). Attr (' checked ', 'true');

Check multiple checkboxes check the first and second checkbox at the same time
$(' input: radio). Slice (0, 2). The attr (" checked ", "true");

7. Set the checkbox as the selected Value according to the Value
$(" input "checkbox (value = '1']"). The attr (' checked ', 'true');

8. Delete the checkbox with Value=1
$(" input "checkbox (value = '1']"). The remove ();

9. Delete the checkbox
$("input:checkbox").eq(index value).remove(); Index = 0,...
For example, delete the third checkbox:
$(" input: the checkbox "). Eq. (2), remove ();

Walk through the checkbox
$(' input: the checkbox). Each (function (index, domEle) {
// write code
});

11. Select all
$(' input: the checkbox). Each (function () {
              $(this). Attr (" checked ", true);
});

12. Cancel all choices
$(' input: the checkbox). Each (function () {
              $(this). Attr (" checked ", false);
});


Related articles: