2016-05-09 35 views
0
<form> 
Chapter 1 - Please enter how many copys you would like <br> 
<input type="text" id="chap1"> <br><br> 
Chapter 2 - Please enter how many copys you would like <br> 
<input type="text" id="chap2"> <br><br> 
Chapter 3 - Please enter how many copys you would like <br> 
<input type="text" id="chap3"> <br><br> 
Chapter 4 - Please enter how many copys you would like <br> 
<input type="text" id="chap4"> <br><br> 
Chapter 5 - Please enter how many copys you would like <br> 
<input type="text" id="chap5"> <br><br> 
    <b> Total price : <output id = "total"> 0 </output> </b> 

乘形式值

我试图创建一个网站中,你可以通过章节订购的书籍,每章花费£2。我需要它将单个表格的值乘以2,然后在输出中显示此成本。我想用java脚本来做到这一点,而不是jQuery。

我还没有尝试过很多,因为我无法找到很多关于这个问题,所以任何指针在正确的方向将不胜感激。

感谢

+0

[雄辩的Javascript(http://eloquentjavascript.net/) – Wainage

+0

给小有点更多的解释你的问题你想要做什么 –

+0

我希望能够让用户定义他们想要的副本数量,例如第1章他们会输入一个数字等 然后我想它的价格乘以数字(2英镑),并显示所有副本加在一起的总数。 –

回答

0
function calc(){ 
    price = 2; 
    fields = document.querySelectorAll("input[id^='chap']"); 
    tot = 0; 
    for(i=0; i<fields.length; i++){ 
     tot += fields[i].value * price; 
    } 
    document.getElementById("total").innerHTML = tot; 
} 

这是一个简单的例子,你就可以改善

+0

它没有工作,我把它放入一个单独的js文件并链接它,但它仍然没有更新总数 –

+0

@SamuelBaxter当总数要更新? 例如,您可以添加一个按钮,调用函数'' – corros

+0

将要做的辉煌的欢呼! –

0

fiddle link

<input type="text" id="chap1"> 
<input type="text" id="chap2"> 
<input type="text" id="chap3"> 
<input type="text" id="chap4"> 
<input type="text" id="chap5"> 
<b> Total price : <output id = "total"> 0 </output> </b> 
<button onclick="myFunction()">Click me</button> 
<script> 
function myFunction() { 
var text1= document.getElementById("chap1").value; 
var text2= document.getElementById("chap2").value; 
var text3= document.getElementById("chap3").value; 
var text4= document.getElementById("chap4").value; 
var text5= document.getElementById("chap5").value; 
var total=parseInt(text1)+parseInt(text2)+parseInt(text3)+parseInt(text4)+parseInt(text5); 
document.getElementById("total").innerHTML = total*2.1; 
} 
</script>