我想将价格输入多个方框,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>
答案 0 :(得分:0)
function test(){
var userInput = (document.getElementById("pkg").value)*1;
var userInput2 = (document.getElementById("pep").value)*1;
document.getElementById("demo").innerHTML = userInput + userInput2;
}
这会起作用!!