-2

anyone here good at js?

im looking for a form with following fields which would auto calculate and display the total on fly when i inpot the numbers in side the fields.

<input type="text" name="acutalprice" value="">Actual Price: 329
<input type="text" name="discount" value="">Discount: 65%
<input type="text" name="shipping" value="">Shipping: 50+

Total Amount: 165.15

on sumbit should get the total amount output

Regards

4

1 回答 1

0

首先,将 jQuery 库添加到<head></head>页面的一部分。您应该使用jQuery 下载页面Google 的托管库页面来查找 jQuery 库。

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

然后,您应该将<input>元素放入<form></form>元素并为表单添加提交按钮。

<form id="myForm">
    <input id="actualprice" type="number" placeholder="Actual Price">
    <input id="discount" type="number" placeholder="Discount">
    <input id="shipping" type="number" placeholder="Shipping">
    <input type="submit" value="Submit">
</form>
<h3 id="result"></h3>

最后,将脚本添加到页面以提交表单。

<script>
    $('#myForm').submit(function (event) {

    event.preventDefault();

    var actualprice = Number($("#actualprice").val().trim());
    var discount = Number($("#discount").val().trim());
    var shipping = Number($("#shipping").val().trim());

    var discountRate = (100 - discount) / 100;
    var result = (actualprice * discountRate) + shipping;

    $("#result").html("Result :" + result.toFixed(2));
});
</script>

祝你好运。

于 2016-02-03T21:45:26.023 回答