1

如何在 keyup 事件而不是在提交时显示输出以及输入字段内的值?

我试过只读<input id="result" name="totalamount" value="" readonly/>但它不工作。有人可以吗?

这是完整的代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<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>


<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  id="result" name="totalamount" value="" readonly/>
    <h3 id="result"></h3>

    <input type="submit" value="Submit">
</form>
4

1 回答 1

1

使用keyupevent forinput并且您对 and 使用相同id result的。我已将of更改为。inputh3idh3result2

$('input').on('keyup input', function () {
  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").val("Result :" + result.toFixed(2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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 id="result" name="totalamount" value="" readonly />
    <h3 id="result2"></h3>

    <input type="submit" value="Submit">
</form>

于 2016-02-05T18:06:34.570 回答