是我计算总数的脚本..
<script type="text/javascript" >
function calculateTotal() {
// price per unit
var priceEL = document.getElementById('price');
// get input element from DOM
var inputEl = document.getElementById('quantity');
// read quantity from the input element (it is read as a string)
var priceSting = priceEL.value;
var quantitySting = inputEl.value;
// convert string value from the form element into integer (or float if needed)
var quantity = parseInt(quantitySting);
var price = parseInt(priceSting);
// calculate total
var total = price * quantity;
// get element that displays total from DOM
var displayEl = document.getElementById('total')
// display total value in target element
displayEl.innerHTML = total;
}
</script>
现在我的代码在div标签内回显总值...如下所示
<div id ="total"></div>
我需要在文本框下面显示总数....
<input type="textbox" name="total" id ="total"></td>
答案 0 :(得分:1)
请记住,对HTML中的所有输入字段使用.value
,innerHTML
用于只读文本字段
供您使用:
displayEl.value= total;
答案 1 :(得分:0)
试试这个:
document.getElementById("total").value = total;
答案 2 :(得分:0)
更改div的id,你不能为两个元素设置相同的ID
<div id ="totaldiv"></div>
并替换你的javascript
displayEl.innerHTML = total;
带
displayEl.value = total;
答案 3 :(得分:0)
根据规范,两个元素不应分配相同的ID属性。
因此,请将您的div ID更改为其他内容并尝试以下代码:
displayEl.value = total;