gpt4 book ai didi

javascript - JS if else 错误

转载 作者:行者123 更新时间:2023-11-28 05:28:43 25 4
gpt4 key购买 nike

当我当前点击提交按钮时,它会更改 if else 语句。程序不应该同时执行这两项操作。当我当前点击提交按钮时,它会更改 if else 语句。程序不应该同时执行这两项操作。当我当前点击提交按钮时,它会更改 if else 语句。该程序不应同时执行这两项操作。`

            var image_tracker='p';
function change(){
var image = document.getElementById('operator');
if(image_tracker=='p'){
image.src = "images/mult.png"
image_tracker='m';
var val1 = parseInt(document.getElementById("value1").value);
var val2 = parseInt(document.getElementById("value2").value);
var val3 = parseInt(document.getElementById("value3").value);
var val4 = parseInt(document.getElementById("value4").value);
var val5 = parseInt(document.getElementById("value5").value);
var ansD = document.getElementById("answer");
ansD.value = val1 * val2 * val3 * val4 * val5;
}else{
image.src='images/plus.png'
image_tracker='p';
var ansD = document.getElementById("answer");
ansD.value = val1 + val2 + val3 + val4 + val5;

}
}
</script>
<body>
<div class="form">
<input type="text" id="value1" name="value1" value=""/>
<div class= value1>
<p>You are entering the 1st Number</p>
</div>
<input type="text" id="value2" name="value2" value=""/>
<div class= value2>
<p>You are entering the 2nd Number</p>
</div>
<input type="text" id="value3" name="value3" value=""/>
<div class= value3>
<p>You are entering the 3rd Number</p>
</div>
<input type="text" id="value4" name="value4" value=""/>
<div class= value4>
<p>You are entering the 4th Number</p>
</div>
<input type="text" id="value5" name="value5" value=""/>
<div class= value5>
<p>You are entering the 5th Number</p>
</div>
<br/>
<input type="button" name="submit" value="Submit" onclick="javascript:change()"/>
<br/>
<img src="images/plus.png" alt="operator" id= "operator" onclick="change()">
<br/>
<input type="label" id="answer" name="answer" value=""/>
</div>

最佳答案

所以,你必须使用它,但是尝试 This Fiddle :

首先,您需要 2 个不同的函数来执行不同的任务。

一个用于运行计算注意:您不需要在两个 if 子句中分配“vals”,因为您使用的是相同的元素。您也不需要 p 和/或 m 变量,因为您只需测试 imgsrc 位置:

function calculateMe() {
var image = document.getElementById('operator');
var val1 = parseInt(document.getElementById("value1").value);
if (isNaN(val1)) { val1 = 0; }
var val2 = parseInt(document.getElementById("value2").value);
if (isNaN(val2)) { val2 = 0; }
var val3 = parseInt(document.getElementById("value3").value);
if (isNaN(val3)) { val3 = 0; }
var val4 = parseInt(document.getElementById("value4").value);
if (isNaN(val4)) { val4 = 0; }
var val5 = parseInt(document.getElementById("value5").value);
if (isNaN(val5)) { val5 = 0; }
var ansD = document.getElementById("answer");
if(image.getAttribute("src") == "~/images/mult.png") {
ansD.value = val1 * val2 * val3 * val4 * val5;
} else {
ansD.value = val1 + val2 + val3 + val4 + val5;
}
}

还有一个更改图像的方法:

function changeIcon() {
var image = document.getElementById('operator');
if (image.getAttribute("src") == "~/images/mult.png") {
image.src = "~/images/plus.png";
} else {
image.src = "~/images/mult.png";
}
}

然后,在 HTML 中,只需调用各个元素上的不同函数即可:

<input type="button" name="submit" value="Submit" onclick="calculateMe()"/>
<img src="~/images/plus.png" alt="operator" id= "operator" onclick="changeIcon()">

您必须尝试使用​​图像位置和确切的 URL,但这就是要点。

关于javascript - JS if else 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39903218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com