从昨晚起我就一直在尝试解决这个问题,但仍然没有成功。我在这方面非常需要帮助。情况是这样的:我有 4 个可点击的图像,每个图像在我的 html 文件上都有一个 clickCount 文本框。 clickCount 文本框将显示图像被单击的次数。我让前 2 个图像正常工作,但是当我单击第三个图像时,第二个图像上的 clickCount 文本框会递增。这是我的代码:
<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>
最佳答案
第一个条件;
if (event.target.id=="img1"){ // Compares
第二个条件;
if (event.target.id="img2"){ // Uses = (ie assigns, not compares)
关于javascript - JQuery if else 语句在第二个条件处停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18265732/