当我点击我的提交按钮时,它会更改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>
答案 0 :(得分:0)
所以,你必须要使用它,但试试This Fiddle:
首先,您需要2个不同的功能来执行不同的任务。
运行计算注意:您不需要分配&#34; vals&#34;在两个if子句中,因为你使用了相同的元素。您也不需要p
和m
变量,因为您只需测试img
的{{1}}位置:
src
一个改变图像:
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;
}
}
然后,在HTML中,只需调用各个元素上的不同函数:
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";
}
}
您必须使用图片位置和确切的网址,但这就是要点。