简体   繁体   中英

Checked checkbox in jquery

I have a varible IDs with 3 checkboxes ids ( #Id1, #Id2, #Id3 ).
So when I do $(IDs) , I have the list of the checkboxes.

How to know how many checkboxes are checked? I want to trigger the click event on the checked checkboxes.

I try $(IDs + ":checked").click() but the :checked is only on #Id3 .

I could do $(IDs).each(...); and do in the function the test if it's checked.
Or change the value of IDs to this : #Id1:checked, #Id2:checked, #Id3:checked .

Is there a way to do it in one line? (or easily)

Thanks

Use filter .

var $checked = $(IDs).filter(':checked');
$checked.click(function() { alert('hello'); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM