JQuery to get id id elements with any of many constants

I need jQuery to get all flags whose identifier ends with PR, PR_KD or PR_KDA. I tried this with no luck:

jQuery("#" + panelInfo[0] + "_row_" + panelInfo[1] + " input:checked").find(":[id$=_chkPR], :[id$=_chkPR_KD], :[id$=_chkPR_KDA])"); 

as well as this:

 jQuery("#" + panelInfo[0] + "_row_" + panelInfo[1] + " input:checked:[id$=_chkPR, id$=_chkPR_KD, id$=_chkPR_KDA]") 

I cannot use the "or" correctly. Can anybody help me?

Thanks in advance!

Alejandro.

+4
source share
3 answers

Your first sample query is closest to the correct syntax, but you use $('... input:checked').find() , which will look for the children of these flags (of which, of course, they won't be).

Therefore, you need to move the input:checked or even better :checkbox:checked , to exclude any radio objects in your .find so that you look in the container you need, and not in the :checkbox:checked list

 jQuery("#" + panelInfo[0] + "_row_" + panelInfo[1]) .find(":checkbox:checked[id$=_chkPR], :checkbox:checked[id$=_chkPR_KD], :checkbox:checked[id$=_chkPR_KDA])"); 

Another way to express this could be:

 jQuery("#" + panelInfo[0] + "_row_" + panelInfo[1]) .find('[id$=_chkPR], [id$=_chkPR_KD], [$=id_chkPR_KDA]') .filter(function() { return $(this).is(':checkbox:checked'); }); 

In the above code, you will find all the elements in the panel with the appropriate identifier and filter out the elements from the installed set that are checked.

You can also eliminate the need to use .find at all if you specify the context instead:

 jQuery(":checkbox:checked[id$=_chkPR], :checkbox:checked[id$=_chkPR_KD], :checkbox:checked[id$=_chkPR_KDA])", document.getElementById(panelInfo[0] + "_row_" + panelInfo[1])); 
+2
source

You are very close. Change find() to this:

 .find(":checkbox:checked[id$=_chkPR], :checkbox:checked[id$=_chkPR_KD], :checkbox:checked[id$=_chkPR_KDA]") 

End Code:

 jQuery("#" + panelInfo[0] + "_row_" + panelInfo[1]).find(":checkbox:checked[id$=_chkPR], :checkbox:checked[id$=_chkPR_KD], :checkbox:checked[id$=_chkPR_KDA]") 

Here's a working fiddle .

+3
source

Source: https://habr.com/ru/post/1401591/


All Articles