如何在keyup事件而不是submit上显示输出以及输入字段中的值?
我尝试了只读<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>
答案 0 :(得分:1)
对keyup
使用input
事件,id
和result
使用相同的input
h3
。我已将id
的{{1}}更改为h3
。
result2
&#13;
$('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));
});
&#13;