2015-05-29 44 views
0

是我的脚本来计算总..javascript代码,以显示内部tetxbox DIV消息下面

<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

试试这个:

document.getElementById("total").value = total; 
0

更改div的ID,则水湿有相同ID的两个元素

<div id ="totaldiv"></div> 

,并在你的JavaScript

displayEl.innerHTML = total; 

更换
displayEl.value = total; 
0

根据说明书,两个要素不应该分配相同的ID属性。

因此改变你的DIV ID到别的东西,尝试下面的代码:

displayEl.value = total; 
1

记住,使用.value是HTML格式的所有输入字段,innerHTML用于只读文本字段

为您的要求使用方法:

displayEl.value= total;

+0

这让我想起了几次,值应该工作 – Izion