添加多个项目

时间:2015-03-12 21:20:40

标签: javascript html

我想将价格输入多个方框,1个方框默认为某个值。以下代码仅将值添加为字符串。我怎么能这样做?

<html>
<head>
<title>Calculator</title>


Package Price <input type="number" id="pkg">      </input><br>
Equipment Package <input type="number" id="pep" value="11.25"></input><br>
    <button onclick="test()">Submit</button>

  <p id="demo"></p>   

<font size="12"> Total </font><br>

<script type="text/javascript">
function test()

{
var userInput = document.getElementById("pkg").value;
var userInput2 = document.getElementById("pep").value;
document.getElementById("demo").innerHTML = userInput + userInput2;
}

</script>

1 个答案:

答案 0 :(得分:0)

function test(){
    var userInput = (document.getElementById("pkg").value)*1;
    var userInput2 = (document.getElementById("pep").value)*1;    
    document.getElementById("demo").innerHTML = userInput + userInput2;
}

这会起作用!!

http://jsfiddle.net/4ou4f6kh/