gpt4 book ai didi

javascript - 无法让 Jquery 将函数的输出写入 html

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:29 24 4
gpt4 key购买 nike

新人来了。我觉得这是一个非常基本的问题,但我无法让这个 Jquery 代码将函数的输出写入 html 并显示出来。我相信这是脚本的问题,但不确定我是否也对 html 做错了什么。

此函数的目标是创建一个计算器,根据用户输入的内容给出 future 的投资值(value)。当我点击提交时,它曾经将所有 html 更改为仅显示“NaN”,但现在当我点击提交按钮时,它会清除输入框但不会给我任何数字或任何答案,即使我相信我已经将其设置为显示号码

HTML

<body>
<div id = "futurevalue">
<form>
<input type="radio" name="formula" value="future_investment" checked>Future Investment<br>
<label for="princeple">Enter the princeple amount invested:</label>
<input id="princeple" type="number" name="princeple" step="0.01"><br>
<label for="time">Enter how long the investment will be for (in years):</label>
<input id='time' type='number' name='time'><br>
<label for='rate'>Enter the expected interest rate:</label>
<input id='rate' type="number" name='rate'><br>
<input id='submit' type='submit' name='submit'>
</form>
<p>Total:</p>
<p id='result'></p>
</div>

jQuery

 $('#submit').click(function(){
$('#result').html(function(){
return toString(output,10);
});
});
var princeple = parseInt($('#princeple'),10);
var time = parseInt($('#time'),10);
var rate = parseInt($('#rate'),10);
var output = (princeple * time + rate);

最佳答案

这是您的解决方法:

$('#submit').click(function(){
var princeple = parseInt($('#princeple').val());
var time = parseInt($('#time').val());
var rate = parseFloat($('#rate').val()/100);
var output = ((princeple * rate) * time);
$('#result').html(output);
//prevent from page reload
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "futurevalue">
<form>
<input type="radio" name="formula" value="future_investment" checked>Future Investment<br>
<label for="princeple">Enter the princeple amount invested:</label>
<input id="princeple" type="number" name="princeple" step="0.01"><br>
<label for="time">Enter how long the investment will be for (in years):</label>
<input id='time' type='number' name='time'><br>
<label for='rate'>Enter the expected interest rate:</label>
<input id='rate' type="number" name='rate'><br>
<input id='submit' type='submit' name='submit'>
</form>
<p>Total:</p>
<p id='result'></p>
</div>

您必须计算 click 事件中的值并分配给 result。计算利息的公式也不正确。

关于javascript - 无法让 Jquery 将函数的输出写入 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45768375/

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