<!DOCTYPE html>
<html lang="pt-br">
<meta charset="utf-8" />
<head>
<title>Vote</title>
</head>
<body>
<div>
Width :
<input type="number" name="" id="blindWidth">
<br> Height :
<input type="number" name="" id="height">
<br> Type :
<input type="text" name="" id="Type">
<br> Quantity :
<input type="number" name="" id="quantity">
<br>
<div id="priceTag">Fill in the inputs above</div>
<div id="dimensions"></div>
</div>
<script type="text/javascript">
function calcPrice(quantityValue) {
quantityValue = quantityValue || 0;
let basePrice = 12;
totalPrice = 12 * quantityValue;
if (totalPrice > 0) {
document.getElementById('priceTag').innerText = `Price : ${totalPrice}`;
} else {
document.getElementById('priceTag').innerText = ``;
}
}
function calcDimensions(heightValue, widthValue) {
let dimensions = heightValue * widthValue;
if (dimensions > 0) {
document.getElementById('dimensions').innerText = `Dimensions : ${dimensions}`;
} else {
document.getElementById('dimensions').innerText = ``;
}
}
let width = document.getElementById('blindWidth');
let height = document.getElementById('height');
let quantity = document.getElementById('quantity');
width.addEventListener('keyup', () => {
// console.log('typing on width');
let widthValue = width.value || 0;
let heightValue = height.value || 0;
let quantityValue = quantity.value || 0;
calcDimensions(widthValue, heightValue);
calcPrice(quantityValue);
})
height.addEventListener('keyup', () => {
// console.log('typing on height');
let widthValue = width.value || 0;
let heightValue = height.value || 0;
let quantityValue = quantity.value || 0;
calcDimensions(widthValue, heightValue);
calcPrice(quantityValue);
})
quantity.addEventListener('keyup', () => {
// console.log('typing on quantity');
let widthValue = width.value || 0;
let heightValue = height.value || 0;
let quantityValue = quantity.value || 0;
calcDimensions(widthValue, heightValue);
calcPrice(quantityValue);
})
</script>
</body>
</html>
Чтобы выполнять вычисления в реальном времени, используйте JavaScript-EventListeners.
Чтобы узнать больше о EventListeners, см. Эту статью
Вот HTML-код, нажмите на RUN SNIPPET, чтобы увидеть, как он работает.
Спасибо.
<!DOCTYPE html>
<html lang="pt-br">
<meta charset="utf-8" />
<head>
<title>Vote</title>
</head>
<body>
<div>
Width :
<input type="number" name="" id="blindWidth">
<br> Height :
<input type="number" name="" id="height">
<br> Type :
<input type="text" name="" id="Type">
<br> Quantity :
<input type="number" name="" id="quantity">
<br>
<div id="priceTag">Fill in the inputs above</div>
<div id="dimensions"></div>
</div>
<script type="text/javascript">
function calcPrice(quantityValue) {
quantityValue = quantityValue || 0;
let basePrice = 12;
totalPrice = 12 * quantityValue;
if (totalPrice > 0) {
document.getElementById('priceTag').innerText = `Price : ${totalPrice}`;
} else {
document.getElementById('priceTag').innerText = ``;
}
}
function calcDimensions(heightValue, widthValue) {
let dimensions = heightValue * widthValue;
if (dimensions > 0) {
document.getElementById('dimensions').innerText = `Dimensions : ${dimensions}`;
} else {
document.getElementById('dimensions').innerText = ``;
}
}
let width = document.getElementById('blindWidth');
let height = document.getElementById('height');
let quantity = document.getElementById('quantity');
width.addEventListener('keyup', () => {
console.log('typing on width');
let widthValue = width.value || 0;
let heightValue = height.value || 0;
let quantityValue = quantity.value || 0;
calcDimensions(widthValue, heightValue);
calcPrice(quantityValue);
})
height.addEventListener('keyup', () => {
console.log('typing on height');
let widthValue = width.value || 0;
let heightValue = height.value || 0;
let quantityValue = quantity.value || 0;
calcDimensions(widthValue, heightValue);
calcPrice(quantityValue);
})
quantity.addEventListener('keyup', () => {
console.log('typing on quantity');
let widthValue = width.value || 0;
let heightValue = height.value || 0;
let quantityValue = quantity.value || 0;
calcDimensions(widthValue, heightValue);
calcPrice(quantityValue);
})
</script>
</body>
</html>