繁体   English   中英

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

[英]Can't get Jquery to write the output of a function to html

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

此功能的目的是创建一个计算器,该计算器根据用户输入的值给出未来的投资价值。 当我单击提交时,它曾经将所有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 计算利息的公式也不正确。

尝试以下代码,基本上是您需要的:计算cb click内的变量,然后将#result输出到div #result 使用.val获取输入值,并使用event.preventDefault()避免表单提交(这是浏览器的默认行为)。

 $('#submit').click(function(event) { var princeple = parseInt($('#princeple').val(), 10); var time = parseInt($('#time').val(), 10); var rate = parseInt($('#rate').val(), 10); var output = (princeple * time + rate); $('#result').html(output); event.preventDefault(); }); 
 <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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM