如何使用javascript添加表单值?

如何使用javascript添加表单值?,javascript,forms,Javascript,Forms,这是我提出的代码,但它所做的只是这个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 = on

这是我提出的代码,但它所做的只是这个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>

函数startCalc(){
间隔=设置间隔(“计算()”,1);
}
函数计算(){
一=document.form1.quantity.value;
二=document.form1.price.value;
c=1+2
document.form1.total.value=(c);
}
函数stopCalc(){
间隔时间;
}
数量:价格:
总计:
当然这是一个非常简单的形式,但是你明白了 请帮我告诉我这里做错了什么

用这个

c = parseInt(one,10) + parseInt(two, 10); 

您需要使用
parseInt()
将字符串转换为整数

c = parseInt(one, 10) + parseInt(two, 10)

您需要将价格值转换为数字

对price使用parseFloat,因为它可以有十进制值

将parseInt与基数一起使用

e、 g:


您可以使用
+
将字符串转换为数字(整数或浮点)

你可以用这个

  one = document.form1.quantity.value/1;
  two = document.form1.price.value/1;

关闭,但不要忘记基数参数,否则会得到一些奇怪的结果。第二个参数10是基数(即基数-10,十进制)。虽然它是默认的基,但还是可以指定它,如果不指定,jslint会抱怨!这是你最好的选择!我对javascript相当陌生,因此我会犯愚蠢的错误;)
+one++two
符号不是更简单吗?
c = +one + +two;
  one = document.form1.quantity.value/1;
  two = document.form1.price.value/1;