繁体   English   中英

javascript 表单使用输入值,否则使用默认值进行即时计算?

[英]javascript form use input value else use default for instant calculation?

我将实体的总成本称为总成本:

var total_cost = 10000;

如果用户没有提供输入,我必须通过手动输入或总成本的百分比来计算另外 2 个成本。 我计算百分比成本为:

var costA = 0.5*total_cost;
var costB = 0.2*total_cost;

想要将页面显示为

Cost A in (Rs.) : 5000 (50% of total cost) or <input type="number" name="costA ">
Cost B in (Rs.) : 2000 (20% of total cost) or <input type="number" name="costB">
Total (Rs.) : 7000 or change as per user input 
Total per item (Rs.) : 700 (if items=10) or change per user input

如果用户更改成本 A 或成本 B 或两者,则总成本和总成本/项目必须自动更改。

那么,在 javascript 中有什么方法可以做到这一点,如果是,请您发布一个示例。 谢谢你。

如果要捕获值更改的事件,请使用以下命令:

这是一个小提琴: https : //jsfiddle.net/Darin_Cardin/7x5rdpLn/

<div>
    <input type="text" >
    Value changed to:<span id="result"></span>

</div>      
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.js"></script>

    <script>


    $('input').keyup(function(i,v){
        var val = $(this).val();
        $('#result').html(val);
    });



    </script>

暂无
暂无

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

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