Home >Web Front-end >JS Tutorial >jQuery get array of checked checkboxes ids
<code class="language-javascript">// 获取所有选中复选框的 ID,并将其存储到一个数组中 var checkedIds = $('input[type="checkbox"]:checked').map(function() { return this.id; }).get(); console.log(checkedIds); </code>
FAQs (FAQs)
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:
<code class="language-javascript">$('input:checkbox').prop('checked', true);</code>
This code will select all check boxes and set their "checked" property to true, thus selecting all check boxes.
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:
<code class="language-javascript">$('input:checkbox').prop('checked', false);</code>
This code will uncheck all check boxes.
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:
<code class="language-javascript">$('input:checkbox:checked').val();</code>
This code will return the value of the first selected checkbox it found.
To get the IDs of all check boxes, you can use the ":checked" selector and the .map()
function. Examples are as follows:
<code class="language-javascript">var ids = $('input:checkbox:checked').map(function() { return this.id; }).get();</code>
This code will return an array containing all selected checkbox IDs.
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:
<code class="language-javascript">$('#myCheckbox').prop('checked', true);</code>
This code will select the check box with ID "myCheckbox".
Similar to the previous question, you can uncheck it using the "#" selector followed by the ID of the check box. Examples are as follows:
<code class="language-javascript">$('#myCheckbox').prop('checked', false);</code>
This code will uncheck the check box with ID "myCheckbox".
To switch the selected state of the check box, you can use the .click()
function. Examples are as follows:
<code class="language-javascript">$('#myCheckbox').click();</code>
This code will toggle the selected state of the check box with ID "myCheckbox".
To check if the checkbox is selected, you can use the .is()
function and the ":checked" selector. Examples are as follows:
<code class="language-javascript">if ($('#myCheckbox').is(':checked')) { // 复选框已选中 } else { // 复选框未选中 }</code>
This code will check whether the check box with ID "myCheckbox" is selected.
To handle the change event of the check box, you can use the .change()
function. Examples are as follows:
<code class="language-javascript">$('#myCheckbox').change(function() { if ($(this).is(':checked')) { // 复选框已选中 } else { // 复选框未选中 } });</code>
This code will handle the change event of the checkbox with ID "myCheckbox".
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:
<code class="language-javascript">$('#myCheckbox1, #myCheckbox2, #myCheckbox3').prop('checked', true);</code>
This code will select the check boxes with IDs "myCheckbox1", "myCheckbox2", and "myCheckbox3".
(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!