html中表格(mygrid)的代码:
<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"> </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>
加载此表(网格)时,我有一个函数可以计算 selectedIndex=1 或 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
);
});
它有效,但计算错误,如果一行中有多个选择元素,则结果只是最后一个选择元素的一个数字,如下图所示: