我對jQuery有點新鮮,只是想知道如何去傳遞字符串值而不是看起來是從選擇器中引用jQuery項目?我很難解釋,所以這裏有一個示例演示。甚至不知道該如何標題,所以如果你能想到更好的標題,請編輯標題。jQuery - 另一個選擇器內的選擇器 - 傳入變量失去它的值併成爲索引器?
在我所在的線上$("td").filter(function(str){
傳入的str成爲TD所在的索引位置。所以在第一次調試時它是0,下一次是1等等。我試過谷歌,但我甚至不知道要搜索什麼,任何文檔/代碼的幫助將不勝感激
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select[name='showTeam']").change(function() {
$("select[name='showTeam'] option:selected").each(function() {
var str = $(this).val().toLowerCase();
//str = what it was set to up there
//alert(str);
$("td").filter(function(str) {
//str = becomes a number = to position of TD.. ie for 5th TD match STR = 4 (starts at index 0)
return $(this).text().toLowerCase().indexOf(str) != -1;
}).css('background','red');
});
})
});
</script>
Show Team: <select id="showTeam" name="showTeam">
<option>All</option>
<option>Chelsea</option>
</select>
<div id="games">
<table border="1">
<tbody>
<tr>
<th>ID</th>
<th>Game date</th>
<th>Field</th>
<th>Home team</th>
<th>Home team score</th>
<th>Away team</th>
<th>Away team score</th>
<th>Game type</th>
</tr>
<tr class="odd_line" id="game_460">
<td>459</td>
<td>03 Nov 19:00</td>
<td>Field 2</td>
<td>Madrid </td>
<td>3</td>
<td>Bayern Munich </td>
<td>1</td>
<td>Season</td>
</tr>
<tr class="odd_line" id="game_461">
<td>460</td>
<td>03 Nov 19:00</td>
<td>Field 3</td>
<td>chelsea</td>
<td>5</td>
<td>arsenal</td>
<td>4</td>
<td>Season</td>
</tr>
</div>
[有關'.filter()']的文檔(http://api.jquery.com/filter/) – bfavaretto 2012-02-16 19:18:17
呃我不知道我是如何錯過的,我想我必須重新思考我的方法..謝謝 – 2012-02-16 19:20:23
你在做什麼?你打算用'filter()'方法實現什麼? – 2012-02-16 19:20:51