I need to find out which items in checkBoxList are selected, and pass them an id;

log = $( ".select_cities :checked" ).val(); $.post("list?id='.'"+log; 

This is how id is passed, but the problem is that my form is dynamic, i.e. I can add exactly the same. The problem is that if the select_cities form select_cities one, everything works, but when I add another one, the value id = id:undefined

Why is this happening? After all, I search by form id, and id does not change depending on the number of elements on the page. Tell me what I'm doing wrong, and how to fix it ??

Here is the code of the div element of the 1st element

enter image description here

Here is the second

enter image description here

It can be seen that only the name changes, but I don’t choose by name, why doesn’t it work then?

    1 answer 1

     $("button").on("click", function() { var log = $( ".select_cities :checked" ).map(function () { return this.value; }).get().join(","); console.log(log); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="select_cities"> <input type="checkbox" value="c1" /> <input type="checkbox" value="c2" /> <input type="checkbox" value="c3" /> <input type="checkbox" value="c4" /> <input type="checkbox" value="c5" /> <input type="checkbox" value="c6" /> <input type="checkbox" value="c7" /> </div> <div class="select_cities"> <input type="checkbox" value="d1" /> <input type="checkbox" value="d2" /> <input type="checkbox" value="d3" /> <input type="checkbox" value="d4" /> <input type="checkbox" value="d5" /> <input type="checkbox" value="d6" /> <input type="checkbox" value="d7" /> </div> <button>click</button> 

    • Thanks, works) - Vlad Shkuta