我试图将两个输入字段相乘,并在总输入元素中显示结果。有人可以告诉我这段代码在哪里吗?
var amount = document.getElementById("amount").value;
var amount = parseInt(amount, 10);
var quantity = document.getElementById("quantity").value;
var quantity = parseInt(quantity, 10);
var total = amount * quantity;
document.getElementById("total").innerHTML = total;
Amount: <input type="number" id="amount" name="amount">
<br> Quantity: <input type="number" id="quantity" name="quantity">
<br> Total: <input type="number" id="total" name="total">
答案 0 :(得分:2)
使用document.getElementById("total").value
代替document.getElementById("total").innerHTML = total
。另外,您需要在特定事件上触发代码。在这里,我刚刚将其设置为在用户使用两个输入字段上的oninput="calc()"
属性向任一输入字段输入值时触发:
function calc() {
var amount = document.getElementById("amount").value;
var amount = parseInt(amount, 10);
var quantity = document.getElementById("quantity").value;
var quantity = parseInt(quantity, 10);
var total = amount * quantity;
document.getElementById("total").value = total;
}
Amount: <input type="number" id="amount" name="amount" oninput="calc();">
<br> Quantity: <input type="number" id="quantity" name="quantity" oninput="calc();">
<br> Total: <input type="number" id="total" name="total">
答案 1 :(得分:1)
可能您需要将事件绑定到按钮(单击)或输入元素(输入)
在此示例中,按钮触发了计算。
以下调整:
.value
用于输入表单元素。Number
或加号+
转换为数字。
document.getElementById('calculate').addEventListener('click', function() {
var amount = document.getElementById("amount").value;
var amount = +amount;
var quantity = document.getElementById("quantity").value;
var quantity = +quantity;
var total = amount * quantity;
document.getElementById("total").value = total;
});
Amount: <input type="number" id="amount" name="amount">
<br> Quantity: <input type="number" id="quantity" name="quantity">
<br>
<br> <input id='calculate' type='button' value='Calculate'>
<br>
<br> Total: <input type="number" id="total" name="total">
答案 2 :(得分:1)
您应该添加一个函数,并在输入更改时调用它
因此请尝试使用oninput
事件
并将document.getElementById("total").innerHTML = total;
更改为document.getElementById("total").value = total;
<html><head></head><body>
Amount: <input type="number" oninput="calculate()" id="amount" name="amount">
<br>
Quantity: <input type="number" oninput="calculate()" id="quantity" name="quantity">
<br>
Total: <input type="number" id="total" name="total">
<script>
function calculate(){
var amount = document.getElementById("amount").value;
var amount = parseInt(amount, 10);
var quantity = document.getElementById("quantity").value;
var quantity = parseInt(quantity, 10);
var total = amount * quantity;
document.getElementById("total").value = total;
}
</script>
</body>
</html>
答案 3 :(得分:-1)
ffmpeg -i mysong.ogg -i coverart.jpg song_with_art.ogg