2012-01-24 26 views
0

我具有下面的表單。使用按鈕,文本字段中的值應該增加或減少。減少按鈕可以正常工作,但增加按鈕只需在數字末尾附加'10',因此100將變爲10010,而不是110。有人可以讓我知道如何解決這個問題嗎?如何添加到文本字段的值

<form name="hello" id="hello"> 
<table border=0> 
<tr> 
<td width=200> 
<input type=text id="input" style="height: 30px; width: 200px" name=amount value=5> 
</td> 
<td> 
<table border=0> 
<tr height=12><td width=20> 
<input type=button value="+" style="height: 15px; width: 25px; text-align:center; vertical-align:center; font-size:8px;" onClick="javascript:this.form.amount.value+=10;"> 
</td></tr> 
<tr height=12><td width=20> 
<input type=button value="-" style="height: 15px; width: 25px; text-align:center; vertical-align:center; font-size:8px;" onClick="javascript:this.form.amount.value-=10;"> 
</td></tr> 
</table> 
</td> 
</tr> 
</table> 
</form> 

回答

2

您的JavaScript將它視爲字符串連接。將該值解析爲一個int,進行數學運算,然後設置該值。

的onclick您+按鈕

onClick="javascript: this.form.amount.value = parseInt(this.form.amount.value, 10) + 10;" 
+0

使用兩個版本的說法,你不希望用戶的輸入被視爲八進制或十六進制。 – Quentin

+0

@昆汀,忘了。謝謝。 – Brandon

+1

注意OP:parseInt的雙參數版本是'parseInt(this.form.amount.value,10)',它表示「我希望這是一個十進制數。」 「parseInt」的第二個參數是符號系統(例如,「10」是十進制的,「16」是十六進制的)。此外,不應將此數字與之後添加的「+ 10」或「-10」混淆。 –

0

的onClick事件中的代碼應閱讀:javascript:this.form.amount.value=Number(this.form.amount.value)+10;

JavaScript沒有一個字符串連接運算符,如PHP,所以你必須強制轉換爲在你做任何算術之前的一個數字。

1

下面是一些代碼,將工作:

<form name="hello" id="hello"> 
<table border=0> 
<tr> 
<td width=200> 
<input type=text id="input" style="height: 30px; width: 200px" name=amount value=5> 
</td> 
<td> 
<table border=0> 
<tr height=12><td width=20> 
<input type=button value="+" style="height: 15px; width: 25px; text-align:center; vertical-align:center; font-size:8px;" onClick="javascript:this.form.amount.value=parseInt(this.form.amount.value,10)+10;"> 
</td></tr> 
<tr height=12><td width=20> 
<input type=button value="-" style="height: 15px; width: 25px; text-align:center; vertical-align:center; font-size:8px;" onClick="javascript:this.form.amount.value=parseInt(this.form.amount.value,10)-10;"> 
</td></tr> 
</table> 
</td> 
</tr> 
</table> 
</form> 
0

您需要強制轉換this.form.amount.value爲整數,否則將被視爲一個字符串。爲此,使用parseInt()

0

JavaScript正在將文本字段的值解釋爲字符串。您可以使用parseInt()將該值轉換爲數字。

javascript:this.form.amount.value=parseInt(this.form.amount.value, 10)+10; 
1

parseInt()是必需的。

使用`parseInt`這

<form name="hello" id="hello"> 
<table border=0> 
<tr> 
<td width=200> 
<input type=text id="input" style="height: 30px; width: 200px" name=amount value=5> 
</td> 
<td> 
<table border=0> 
<tr height=12><td width=20> 
<input type=button value="+" style="height: 15px; width: 25px; text-align:center; vertical-align:center; font-size:8px;" onClick="javascript: this.form.amount.value = parseInt(this.form.amount.value) + 10;"> 
</td></tr> 
<tr height=12><td width=20> 
<input type=button value="-" style="height: 15px; width: 25px; text-align:center; vertical-align:center; font-size:8px;" onClick="javascript:this.form.amount.value-=10;"> 
</td></tr> 
</table> 
</td> 
</tr> 
</table> 
</form> 
相關問題