使用HTML和JavaScript的折扣计算器

时间:2013-11-17 06:21:17

标签: javascript html5

我正在使用此代码将代码设置为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);

    }

1 个答案:

答案 0 :(得分:2)

我刚试过你的代码,它运行正常。看看你有什么遗失

<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>