我做了一个年龄计算器,我需要刷新才能重用
I made an age calulator and i need to refresh to reuse
我是 java 脚本的新手,正在摆弄年龄计算器。我做了一个我发现效果很好的,但我注意到如果你要使用它,你必须刷新页面才能再次使用它。我该怎么做才能让您不需要刷新,只需输入另一个值即可?
<table id="tableCenter">
<tr id="yearBornRow">
<p>What year were they born?</p>
<input id="yearBorn">
</tr>
<tr id="currentYearRow">
<p>What is the current year?</p>
<input id="currentYear">
</tr>
</table>
<br>
<button onclick="showAge()">Submit</button>
<p id="printCurrentAge">Submit to find the age!</p>
然后是 js
<script>
var yearBorn = document.getElementById("yearBorn").value
var currentYear = document.getElementById("currentYear").value
var age = currentYear - yearBorn
var possiblity = age++
function showAge(){
document.getElementById("printCurrentAge").innerHTML = "The person is either " +possiblity+ " or " +age;
}
</script>
感谢您在高级方面的帮助
由于您要对值执行算术运算,因此需要将它们正确解析为整数,以便可以通过 parseInt()
函数使用它们:
var yearBorn = parseInt(document.getElementById("yearBorn").value);
var currentYear = parseInt(document.getElementById("currentYear").value);
var age = currentYear - yearBorn
var possiblity = age + 1;
您还需要将此添加到 showAge()
函数中,以便在每次单击按钮时计算它:
function showAge(){
var yearBorn = parseInt(document.getElementById("yearBorn").value);
var currentYear = parseInt(document.getElementById("currentYear").value);
var age = currentYear - yearBorn
var possiblity = age + 1;
document.getElementById("printCurrentAge").innerHTML = "The person is either " +possiblity+ " or " +age;
}
您只需将变量移动到函数内部即可。它会像魅力一样工作!喜欢以下:
<script>
function showAge(){
var yearBorn = document.getElementById("yearBorn").value
var currentYear = document.getElementById("currentYear").value
var age = currentYear - yearBorn
var possiblity = age++
document.getElementById("printCurrentAge").innerHTML = "The person is either " +possiblity+ " or " +age;
}
</script>
我是 java 脚本的新手,正在摆弄年龄计算器。我做了一个我发现效果很好的,但我注意到如果你要使用它,你必须刷新页面才能再次使用它。我该怎么做才能让您不需要刷新,只需输入另一个值即可?
<table id="tableCenter">
<tr id="yearBornRow">
<p>What year were they born?</p>
<input id="yearBorn">
</tr>
<tr id="currentYearRow">
<p>What is the current year?</p>
<input id="currentYear">
</tr>
</table>
<br>
<button onclick="showAge()">Submit</button>
<p id="printCurrentAge">Submit to find the age!</p>
然后是 js
<script>
var yearBorn = document.getElementById("yearBorn").value
var currentYear = document.getElementById("currentYear").value
var age = currentYear - yearBorn
var possiblity = age++
function showAge(){
document.getElementById("printCurrentAge").innerHTML = "The person is either " +possiblity+ " or " +age;
}
</script>
感谢您在高级方面的帮助
由于您要对值执行算术运算,因此需要将它们正确解析为整数,以便可以通过 parseInt()
函数使用它们:
var yearBorn = parseInt(document.getElementById("yearBorn").value);
var currentYear = parseInt(document.getElementById("currentYear").value);
var age = currentYear - yearBorn
var possiblity = age + 1;
您还需要将此添加到 showAge()
函数中,以便在每次单击按钮时计算它:
function showAge(){
var yearBorn = parseInt(document.getElementById("yearBorn").value);
var currentYear = parseInt(document.getElementById("currentYear").value);
var age = currentYear - yearBorn
var possiblity = age + 1;
document.getElementById("printCurrentAge").innerHTML = "The person is either " +possiblity+ " or " +age;
}
您只需将变量移动到函数内部即可。它会像魅力一样工作!喜欢以下:
<script>
function showAge(){
var yearBorn = document.getElementById("yearBorn").value
var currentYear = document.getElementById("currentYear").value
var age = currentYear - yearBorn
var possiblity = age++
document.getElementById("printCurrentAge").innerHTML = "The person is either " +possiblity+ " or " +age;
}
</script>