2017-06-20 56 views
0

當它運行它只是不會返回一個值,我不明白爲什麼。當我在html文件中運行它時,它似乎工作正常。我的HTML表單不會返回數組中的值嗎?

此外,當數字爲5例如(數字+數字)已返回55而不是10.我認爲這是因爲我有引號,但我一次又一次地檢查,事實並非如此。

幫助非常感謝

HTML

<body> 

<form id = 'table' name = 'table'> 
<p> Gimme a number </p> 
<input id = 'textbox' type = 'number' name = 'inputBox'> 
<input id = 'button' type = 'button' value = 'Submit' onclick = 'check()'> 

</form> 
</body> 

的Javascript

function check(){ 
    var number = document.table.inputBox.value; 
var array = [number]; 
array.push(number+number); 
alert(array); 

https://jsfiddle.net/67tt6te7/

+0

讀取的字符串,解析它作爲一個整數:'var number = parseInt(document.table.inputBox.value,10);' –

+0

你的小提琴不工作,因爲你的小提琴設置錯誤。 JavaScript不在全局範圍內。點擊齒輪圖標並將負載類型更改爲正文或正文。 – epascarello

回答

1

其返回55,因爲它告訴你問什麼就其加入2串彼此「 5「+」5「是」55「

如果要總結自己的數值,你需要使用parseInt函數功能,將它們轉換成數第一

parseInt函數(數)+ parseInt函數(數)會給你10