Jquery: Calculating values form multiple select elements

Jquery: Calculating values form multiple select elements

code for table(mygrid) in html:

    <table cellspacing="0" border="1" style="border-collapse:collapse;" id="mygrid" 
                                                            rules="all">
    <tbody>
    <tr>
    <th scope="col">Nr de ord</th>
    <th scope="col" class="hideGridColumn">&nbsp;</th>
    <th scope="col" class="hideGridColumn">StudentId</th>
    <th scope="col">Numele Prenuele</th>
    <th scope="col">
                               <span id="mondayText">Luni<br></span>
                </th>
    <th scope="col">
                               <span id="thuesdayText">Marti<br></span>
                               <span id="thuesday">04.09.2012</span>
                </th>
    <th scope="col">
                               <span id="wednesdayText">Miercuri<br></span>
                               <span id="wednesday">05.09.2012</span>
                </th>
    <th scope="col">
                               <span id="thursdayText">Joi<br></span>
                               <span id="thursday">06.09.2012</span>
                </th>
    <th scope="col">
                               <span id="fridayText">Vineri<br></span>
                               <span id="friday">07.09.2012</span>
                </th>
    <th scope="col">
                               <span id="saturdayText">Simbata<br></span>
                               <span id="saturday">08.09.2012</span>
                </th>
    <th scope="col">
                               <span id="absM">Absente motivate</span>
                </th>
    <th scope="col">
                              <span id="absN">Absente nemotivate</span>
                </th>
    </tr>
    <tr>
    <td>1</td>
    <td class="hideGridColumn">9201001121311</td>
    <td class="hideGridColumn">120001</td>
    <td>asd asd asd</td>
    <td><select class="ddlJ" id="a1_0" 
                              name="ctl00$contentbody$mygrid$ctl02$a1">
    <option value="a">a</option>
    <option value="m" selected="selected">m</option>
    <option value="n">n</option>
        </select>
                </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td>
                    <span id="totalM">1</span>
                </td>
    <td>
                    <span id="totalN">0</span>
                </td>
    </tr>
    <tr>
    <td>2</td>
    <td class="hideGridColumn">9201001121311</td>
    <td class="hideGridColumn">120002</td>
    <td>test1  </td>
    <td><select class="ddlJ" id="a1_1" 
                              name="ctl00$contentbody$mygrid$ctl03$a1">
    <option value="a">a</option>
    <option value="m">m</option>
    <option value="n" selected="selected">n</option>
       </select>
                </td>
    <td></td>
    <td><select class="ddlJ" id="a111_1" 
                                name="ctl00$contentbody$mygrid$ctl03$a111">
    <option value="a">a</option>
    <option value="m" selected="selected">m</option>
    <option value="n">n</option>
          </select>
                </td>
    <td></td>
    <td></td>
    <td></td>
    <td><span id="totalM">1</span>
                </td>
    <td><span id="totalN">0</span>
                </td>
    </tr>
    <tr>
    <td>3</td>
    <td class="hideGridColumn">9201001121311</td>
    <td class="hideGridColumn">120003</td>
    <td>test3  </td>
    <td><select class="ddlJ" id="a1_2" 
                                name="ctl00$contentbody$mygrid$ctl04$a1">
    <option value="a">a</option>
    <option value="m" selected="selected">m</option>
    <option value="n">n</option>
          </select>
                </td>
    <td></td>
    <td><select class="ddlJ" id="a111_2" 
                              name="ctl00$contentbody$mygrid$ctl04$a111">
    <option value="a">a</option>
    <option value="m">m</option>
    <option value="n" selected="selected">n</option>
        </select>
                </td>
    <td></td>
    <td></td>
    <td></td>
    <td><span id="totalM">0</span>
                </td>
    <td><span id="totalN">1</span>
                </td>
    </tr>
    </tbody></table>

When this table(grid) is loaded I have a function that calculates number of selectedIndex=1 or slectedIndex=2.

    var collection = $('select.ddlJ');
    console.log(collection);
                $.each(collection, function(key,value)
                
                {
                        console.log("key:"+key+"si value"+value);
                       
                         var p = $(value).parent().parent();
                         p.find('td:last').prev().find('span').html(
                         p.find($(value)).filter(function () {
                         return $.trim($(value).get(0).selectedIndex) == 1;
                         }).length
                        );
    
                           p.find('td:last span').html(
                           p.find($(value)).filter(function () {
                           return $.trim($(value).get(0).selectedIndex) == 2;
                           }).length
                         );
                 
          });      

It works but it calculates wrong, if there are multiple select elements in a row the result is just a number from the last select element, just like in the picture below: