Home > Web Front-end > JS Tutorial > jQuery get array of checked checkboxes ids

jQuery get array of checked checkboxes ids

Christopher Nolan
Release: 2025-02-26 08:55:08
Original
241 people have browsed it

// 获取所有选中复选框的 ID,并将其存储到一个数组中
var checkedIds = $('input[type="checkbox"]:checked').map(function() {
  return this.id;
}).get();

console.log(checkedIds); 
Copy after login

FAQs (FAQs)

How to use jQuery to select all check boxes?

Use jQuery to select all check boxes, you can use the ":checkbox" selector. This selector selects all elements of type check box. Examples are as follows:

$('input:checkbox').prop('checked', true);
Copy after login

This code will select all check boxes and set their "checked" property to true, thus selecting all check boxes.

How to uncheck all check boxes using jQuery?

Similar to the previous question, you can use the ":checkbox" selector to select all check boxes. To uncheck all check boxes, set the "checked" property to false. Examples are as follows:

$('input:checkbox').prop('checked', false);
Copy after login

This code will uncheck all check boxes.

How to use jQuery to get the value of the selected checkbox?

To get the value of the checkbox selected, you can use the ":checked" selector. This selector selects all selected check boxes. Examples are as follows:

$('input:checkbox:checked').val();
Copy after login

This code will return the value of the first selected checkbox it found.

How to use jQuery to get the ID of all checkboxes?

To get the IDs of all check boxes, you can use the ":checked" selector and the .map() function. Examples are as follows:

var ids = $('input:checkbox:checked').map(function() {
  return this.id;
}).get();
Copy after login

This code will return an array containing all selected checkbox IDs.

How to use jQuery to select a checkbox based on ID?

To select a check box based on the ID, you can use the ID of the "#" selector followed by the check box. Examples are as follows:

$('#myCheckbox').prop('checked', true);
Copy after login

This code will select the check box with ID "myCheckbox".

How to use jQuery to uncheck a checkbox based on ID?

Similar to the previous question, you can uncheck it using the "#" selector followed by the ID of the check box. Examples are as follows:

$('#myCheckbox').prop('checked', false);
Copy after login

This code will uncheck the check box with ID "myCheckbox".

How to use jQuery to switch the selected state of the check box?

To switch the selected state of the check box, you can use the .click() function. Examples are as follows:

$('#myCheckbox').click();
Copy after login

This code will toggle the selected state of the check box with ID "myCheckbox".

How to use jQuery to check if the checkbox is selected?

To check if the checkbox is selected, you can use the .is() function and the ":checked" selector. Examples are as follows:

if ($('#myCheckbox').is(':checked')) {
  // 复选框已选中
} else {
  // 复选框未选中
}
Copy after login

This code will check whether the check box with ID "myCheckbox" is selected.

How to use jQuery to handle change events of check boxes?

To handle the change event of the check box, you can use the .change() function. Examples are as follows:

$('#myCheckbox').change(function() {
  if ($(this).is(':checked')) {
    // 复选框已选中
  } else {
    // 复选框未选中
  }
});
Copy after login

This code will handle the change event of the checkbox with ID "myCheckbox".

How to use jQuery to select multiple checkboxes based on ID?

To select multiple check boxes based on ID, you can use the "#" selector followed by the ID of the check box, separated by commas. Examples are as follows:

$('#myCheckbox1, #myCheckbox2, #myCheckbox3').prop('checked', true);
Copy after login

This code will select the check boxes with IDs "myCheckbox1", "myCheckbox2", and "myCheckbox3".

jQuery get array of checked checkboxes ids (The picture remains as it is)

The above is the detailed content of jQuery get array of checked checkboxes ids. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template