我一直在試圖解決這個問題,因爲昨晚仍然沒有運氣。我非常需要幫助。情況如下:我有4個可點擊的圖片,每個圖片在我的html文件上都有一個clickCount文本框。 clickCount文本框將顯示圖像被點擊的次數。我得到了前2張圖片,但是當我點擊第三張圖片時,第二張圖片上的clickCount文本框會增加。這是我的代碼:JQuery if else語句在第二種情況下停止
<script>
var targetId="";
$(".foods").click(function(event) {
if (event.target.id=="img1"){
targetId="img1clickCount";
addOrders();}
else if(event.target.id="img2"){
targetId="img2clickCount";
addOrders(); }
else if(event.target.id="img3"){
targetId="img3clickCount";
addOrders(); }
else if(event.target.id="img4"){
targetId="img4clickCount";
addOrders(); }
}); });
function addOrders(){
document.getElementById(targetId).value=parseInt(document.getElementById(targetId).
value)+1;
}
</script>
<body>
<table cellpadding=0 cellspacing=0 border=1 height=330 style="margin-left:18px">
<tr>
//Images
<td><img src="gangjeong.png" width="100" style="cursor:pointer" id="img1"
class="foods"></td>
<td><img src="daktoritang.png" width="100" style="cursor:pointer" id="img2"
class="foods"></td>
<td><img src="tangsuyuk.png" width="100" style="cursor:pointer" id="img3"
class="foods"></td>
<td><img src="tangsuyuk.png" width="100" style="cursor:pointer" id="img4"
class="foods"></td>
<tr height="5">
//Textboxes
<td><input type="text" maxlength="5" readonly="readonly" value="0" id="img1clickCount">
</td>
<td><input type="text" maxlength="5" readonly="readonly" value="0" id="img2clickCount">
</td>
<td><input type="text" maxlength="5" readonly="readonly" value="0" id="img3clickCount">
</td>
<td><input type="text" maxlength="5" readonly="readonly" value="0" id="img4clickCount">
</td>
</tr></table>
哦,我是多麼白癡啊。非常感謝您的快速回復。現在正在工作。乾杯! – Clyde