我使用JS函数添加两个数字,当我第一次将值设为3 + 4时,我的输出为"你的答案是7"直到这里它很好。但是当我将10 + 10的不同值放在一起时没有刷新,那么我的输出变为"你的答案是7 10" 它不会替换7而是将它添加到该段落我需要在代码中进行更改,以便每次都能获得一个值答案。
非常感谢, Dhanajay
<p> Add two number </p>
<label for="value1">Enter Number 1</label>
<input type="text" id="value1" />
<br />
<br />
<label for="value2">Enter Number 2</label>
<input type="text" id="value2" />
<br />
<br />
<button id="calculate" type="button" onclick="myFunction()">Calculate</button>
<p id="answer">Your answer is </p>
<script type="text/javascript">
function myFunction() {
var y = document.getElementById("value1").value;
var z = document.getElementById("value2").value;
var x = +y + +z;
document.getElementById("answer").innerHTML = document.getElementById("answer").innerHTML + " " + x;
}
</script>
答案 0 :(得分:2)
<p> Add two number </p>
<label for="value1">Enter Number 1</label>
<input type="text" id="value1" />
<br />
<br />
<label for="value2">Enter Number 2</label>
<input type="text" id="value2" />
<br />
<br />
<button id="calculate" type="button" onclick="myFunction()">Calculate</button>
<p id="answer">Your answer is </p>
<script type="text/javascript">
function myFunction() {
var y = document.getElementById("value1").value;
var z = document.getElementById("value2").value;
var x = (+y + +z);
document.getElementById("answer").innerHTML = x;
}
</script>
答案 1 :(得分:1)
使用另一个元素来保持这样的答案:
function myFunction() {
var y = document.getElementById("value1").value;
var z = document.getElementById("value2").value;
var x = +y + +z;
document.getElementById("output").innerText = x;
}
<p>Add two number</p>
<label for="value1">Enter Number 1</label>
<input type="text" id="value1" />
<br/>
<br/>
<label for="value2">Enter Number 2</label>
<input type="text" id="value2" />
<br/>
<br/>
<button id="calculate" type="button" onclick="myFunction()">Calculate</button>
<p id="answer">Your answer is <span id="output"></span>
</p>
答案 2 :(得分:1)
希望这会有用
function myFunction() {
var y=null;
var z=null;
y = document.getElementById("value1").value;
z = document.getElementById("value2").value;
var x = parseInt(y) + parseInt(z);
document.getElementById("ans").innerHTML = x;
}
HTML
<p> Add two number </p>
<label for="value1">Enter Number 1</label>
<input type="text" id="value1" />
<br />
<br />
<label for="value2">Enter Number 2</label>
<input type="text" id="value2" />
<br />
<br />
<button id="calculate" type="button" onclick="myFunction()">Calculate</button>
<p>Your answer is </p> <span id='ans'></span>
答案 3 :(得分:0)
改变这个:
document.getElementById("answer").innerHTML = document.getElementById("answer").innerHTML + " " + x;
到此:
document.getElementById("answer").innerHTML = x;
答案 4 :(得分:0)
你的最后一行说明了这个
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
将结果附加到字段。
将其更改为
document.getElementById("answer").innerHTML = document.getElementById("answer").innerHTML + " " + x;
答案 5 :(得分:0)
您需要解析回答int的值,然后添加x以累积您的答案加上新的数字:
document.getElementById("answer").innerHTML = parseInt(document.getElementById("answer").innerHTML) + x;
答案 6 :(得分:0)
var x=0;
function myFunction() {
var y = document.getElementById("value1").value;
var z = document.getElementById("value2").value;
x += parseInt(y) + parseInt(z);
console.log(x);
document.getElementById("answer").innerHTML = document.getElementById("answer").innerHTML + " " + x;
}
答案 7 :(得分:0)
替换此
document.getElementById("answer").innerHTML = document.getElementById("answer").innerHTML + " " + x;
用这个
document.getElementById("answer").innerHTML = " Your answer is " + x;
答案 8 :(得分:-1)
您需要在计算后清除变量。或者你只是附加当前的定义
...
var x = +y + +z;
delete y;
delete z;