我有以下代碼,哪種工作,除了我得到相同的答案在所有行上重複。jQuery .each()重複所有行的答案
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>each demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<span>(click here to change)</span>
<table width="50%" border="0" cellspacing="2" cellpadding="2">
<tr>
<th scope="col">Part Number</th>
<th scope="col">Result</th>
</tr>
<tr>
<td id="pn">84ps01</td>
<td class="div1"> </td>
</tr>
<tr>
<td id="pn">92K002</td>
<td class="div1"> </td>
</tr>
<tr>
<td id="pn">68F017</td>
<td class="div1"> </td>
</tr>
</table>
<script>
$("span").click(function() {
var pn = $("#pn").text();
$("td#pn").each(function() {
$(".div1").load("data.asp?prodref="+($(this).text()))
});
});
</script>
$(this).("#div1").load("data.asp?prodref="+pn+"#result");
</body>
</html>
data.asp
是一個簡單的頁面,查詢使用request.querystring("prodref")
返回結果的數據庫。
所以我想獲取表的每一行來查詢數據庫並返回結果。
這裏'$(」。DIV1 「)。負載(」 data.asp?prodref = 「+($(this).text()))''''在'.each()'函數的每次迭代中,您將數據加載到每個具有'div1'類的元素中。這不是jQuery的錯,jQuery完全按照你的要求去做。 –