这是我提出的代码,但它所做的只是1 + 1 = 11我需要它做1 + 1 = 2.
<head>
<script type="text/javascript">
function startCalc(){
interval = setInterval("calc()",1);
}
function calc(){
one = document.form1.quantity.value;
two = document.form1.price.value;
c = one + two
document.form1.total.value = (c);
}
function stopCalc(){
clearInterval(interval);
}
</script>
</head>
<body>
<form name="form1">
Quantity: <input name="quantity" id="quantity" size="10">Price: <input name="price" id="price" size="10"><br>
Total: <input name="total" size="10" readonly=true><br>
<input onclick="startCalc();" onmouseout="stopCalc()" type="button" value="Submit">
</form>
</body>
Run Code Online (Sandbox Code Playgroud)
当然这是一个非常简单的形式,但你明白了,请帮我告诉我这里做错了什么
您需要使用parseInt()
将字符串转换为整数.
c = parseInt(one, 10) + parseInt(two, 10)
Run Code Online (Sandbox Code Playgroud)