gpt4 book ai didi

javascript - 使用 AJAX 的非常基本的数学脚本?

转载 作者:行者123 更新时间:2023-11-28 19:33:15 26 4
gpt4 key购买 nike

我想问是否可以使用 AJAX 在表单的输入文本字段中进行非常基本的数学运算?我知道它可以使用 javascript/jquery,但我不想必须单击提交按钮并重新加载页面才能查看结果。我想要在输入文本字段中进行数学计算并立即得到结果。

示例:

输入文本字段 1 + 输入文本字段 2 =(在输入文本字段 3 中显示即时结果)

最佳答案

您可以执行以下操作

http://jsfiddle.net/dharam1987/L9evdhkk/

Val 1 <input type="text" name="f1" id="f1" /> <br />
Val 2 <input type="text" name="f2" id="f2" /> <br />
Result <input type="text" name="res" id="res" /> <br />
<input type="button" value="Add" onclick="calculate();" />

<script>
function calculate() {
var f1 = parseInt(document.getElementById('f1').value);
var f2 = parseInt(document.getElementById('f2').value);
var res = f1 + f2;
document.getElementById('res').value = res;
}
</script>

关于javascript - 使用 AJAX 的非常基本的数学脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357909/

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