我有選擇的元素集合,當我改變了第一次一個選擇元素的值這個功能不工作,它的工作原理我第二次更改值和控制檯我有Jquery:不推薦使用屬性的指定屬性。它總是返回true
jQuery代碼:
$(document).ready(function() {
var collection = $('select.ddlJ');
console.log(collection);
for (var element in collection) {
$(element).change(function() {
$('select.ddlJ').change(function (e) {
$(this).parent().parent().find('td:last').prev().find('span').html(
$(this).parent().parent().find('select.ddlJ').filter(function() {
return $.trim($(this).val()) == 'm';
}).length);
$(this).parent().parent().find('td:last span').html(
$(this).parent().parent().find('select.ddlJ').filter(function() {
return $.trim($(this).val()) == 'n'; }).length);
});
});
}
});
HTML代碼:
<table cellspacing="0" border="1" style="border-collapse:collapse;" id="grid1"
rules="all">
<tbody><tr>
<th scope="col">Nr de ord</th>
<th scope="col">StudentId</th>
<th scope="col">Name LName</th>
<th scope="col">
<span id="mondayText">monday<br></span>
<span id="monday">14.05.2012</span>
</th>
<th scope="col">
<span id="thuesdayText">thuesday<br></span>
<span id="thuesday">15.05.2012</span>
</th>
<th scope="col">
<span id="wednesdayText">wednesday<br></span>
<span id="wednesday">16.05.2012</span>
</th>
<th scope="col">
<span id="thursdayText">thursday<br></span>
<span id="thursday">17.05.2012</span>
</th>
<th scope="col">
<span id="fridayText">friday<br></span>
<span id="friday">18.05.2012</span>
</th>
<th scope="col">
<span id="saturdayText">saturday<br></span>
<span id="saturday">19.05.2012</span>
</th>
<th scope="col">
<span id="M">Total1</span>
</th>
<th scope="col">
<span id="N">Total2</span>
</th>
</tr><tr>
<td> 1 </td>
<td>110001</td>
<td>Test1 Test1</td><td>
</td><td>
</td><td>
</td><td>
<select class="ddlJ" id="a1111_0" name="ctl00$contentbody$grid1$ctl02$a1111">
<option value="a" selected="selected">a</option>
<option value="m">m</option>
<option value="n">n</option>
</select>
<select class="ddlJ" id="a2222_0" name="ctl00$contentbody$grid1$ctl02$a2222">
<option value="a" selected="selected">a</option>
<option value="m">m</option>
<option value="n">n</option>
</select>
</td><td>
</td><td>
</td><td>
<span class="label" id="totalM"></span>
</td><td>
<span id="totalN"></span>
</td>
</tr><tr>
<td> 2 </td>
<td>110002</td>
<td>Test2 Test2</td>
<td></td>
<td></td>
<td></td><td>
<select class="ddlJ" id="a1111_1" name="ctl00$contentbody$grid1$ctl03$a1111">
<option value="a" selected="selected">a</option>
<option value="m">m</option>
<option value="n">n</option>
</select>
</td><td>
</td><td>
</td><td>
<span class="label" id="totalM"></span>
</td><td>
<span id="totalN"></span>
</td>
</tr><tr>
<td>
3
</td><td>110008</td><td>Test3 Test3</td><td>
</td><td>
</td><td>
</td><td>
<select class="ddlJ" id="a1111_2" name="ctl00$contentbody$grid1$ctl04$a1111">
<option value="a" selected="selected">a</option>
<option value="m">m</option>
<option value="n">n</option>
</select>
</td><td>
</td><td>
</td><td>
<span class="label" id="totalM"></span>
</td><td>
<span id="totalN"></span>
</td>
</tr>
</tbody>
</table>
請張貼您代碼的相關部分。你確定這個通知是由改變這些元素的值(或者可能是別的)引起的嗎? –
順便說一句,而不是使用.parent()。parent(),你可能想用.end()來「撤消」以前的「破壞性」鏈式操作。我相信你會以這種方式獲得更好的表現,儘管我沒有真正衡量這一點。 http://api.jquery.com/end/ – iX3
請包括相關的HTML以及JavaScript/jQuery。爲了更快得到更好的答案,我強烈建議在JS Fiddle這樣的在線工具上發佈一個演示問題的例子:http://jsfiddle.net/ – iX3