На странице есть форма с 3 полями: Price, Amount, Total. Как сделать, чтобы после ввода Price и Amount, Total подсчитывался автоматически по какой-либо формуле?
$(document).ready(() => {
function recalculate(price, amount) {
if (!price || !amount) {
$('#total').val('');
return;
};
let total = price * amount;
$('#total').val(total);
};
$('#price, #amount').on('input', () => {
let price = $('#price').val(),
amount = $('#amount').val();
recalculate(+price, +amount);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
price: <input id="price"> <br>
amount: <input id="amount"> <br>
total: <input id="total" readonly>
<input id="price" type="text" name="">
<input id="amount" type="text" name="">
<input id="total" type="text" name="">
<script type="text/javascript">
var price=document.getElementById("price");
var amount=document.getElementById("amount");
var total=document.getElementById("total");
price.addEventListener("input",calc);
amount.addEventListener("input",calc);
function calc(){
total.value=price.value*amount.value;
}
</script>
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости