使用 HTML 和 javascript 的折扣计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20027865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:32:29  来源:igfitidea点击:

Discount Calculator using HTML and javascript

javascripthtml

提问by user3001030

I am using this code to set a value to a html textbox the code works but the values appears and then disappear immediately.

我正在使用此代码为 html 文本框设置一个值,该代码有效,但这些值出现然后立即消失。

 <script type="text/javascript">
    function calculate() {
       var price=0, discount=0, afterDiscount=0;
        price = Number(document.discountCalculator.price.value);
        discount = Number(document.discountCalculator.discount.value);


     afterDiscount=price - ( price*discount/100 );

       document.discountCalculator.price.value=price.toFixed(2);
       document.discountCalculator.discount.value=discount.toFixed(2);
       document.discountCalculator.afterDiscount.value=afterDiscount.toFixed(2);

    }

回答by Ali Sarfaraz

i just tried your code and it works fine. see if you have anything missing

我刚刚试过你的代码,它工作正常。看看你有没有遗漏什么

<html>
<head>
<script type="text/javascript">
function calculate() {
   var price=0, discount=0, afterDiscount=0;
    price = Number(document.discountCalculator.price.value);
    discount = Number(document.discountCalculator.discount.value);


 afterDiscount=price - ( price*discount/100 );

   document.discountCalculator.price.value=price.toFixed(2);
   document.discountCalculator.discount.value=discount.toFixed(2);
   document.discountCalculator.afterDiscount.value=afterDiscount.toFixed(2);

}
</script>
</head>
<body>
<form name="discountCalculator" >
    <input type="text" name="price" >
    <input type="text" name="discount" >
    <input type="text" name="afterDiscount" >
</form>
<input type="button" value="submit" onclick="calculate()">
</body>
</html>