将两个输入字段一起添加



我一直试图从两个输入字段添加两个值与一个简单的js函数在一起。不工作,请帮忙。

<input class = "field1" type="text">
<input class = "field2" type="text">
<p class = "field3"></p>
<button onclick = "calculate()">Calculate</button>
<script>
function calculate(){
let ipt1 = document.getElementsByClassName("field1").value;
let ipt2 = document.getElementsByClassName("field2").value;
let results = document.getElementsByClassName("field3");
let total = Number(ipt1 + ipt2);
return results.innerHTML = total;
}
</script>
<input class = "field1" type="text">
<input class = "field2" type="text">
<p class = "field3"></p>
<button onclick = "calculate()">Calculate</button>
<script>
function calculate(){
let ipt1 = document.getElementsByClassName("field1")[0].value
let ipt2 = document.getElementsByClassName("field2")[0].value
let results = document.getElementsByClassName("field3")[0]
let total = Number(ipt1) + Number(ipt2)
return results.innerHTML = total;
}
</script>

相关内容

  • 没有找到相关文章

最新更新