2012-08-14 83 views
-1

`嗨,夥計們。我創建一個程序,允許用戶將數據輸入到文本框中,然後將數據傳遞到不同的函數中,並返回結果以用於測試目的。如何在JavaScript函數中傳遞多個參數並打印結果?

他們大多數工作,但即時通訊數據與功能,採取2個或更多變量傳遞數據時遇到困難。

多參數功能只會打印1功能,然後停止。其餘的工作正常。

這裏有一個例子

代碼:

function testOne(1) 
{} 

function testTwo(1,2) 
{} 

function testThree(1,2,3) 
{} 

function printTests() 
{ 
document.writeln("testOne" + testOne(inputOne.value)) //Works fine 
document.writeln("testTwo" + testTwo(inputOne.value, inputTwo.value)) //Stops here 
document.writeLn("testThree" + testThree(inputOne.value, inputTwo.value, 
inputThree.value)) //doesnt work, it stops at whatever method has 2 variables, prints it and then stops. 
} 

我的輸入是這樣的。

<input type="text" id="inputOne"> 
<input type="text" id="inputTwo> 
<input type="text" id="inputThree"> 

所以基本上我只想讓我的文本值通過每種方法傳遞,並顯示按鈕單擊返回。但是,如果函數需要2個或更多變量,則它不起作用,因爲它會正確地打印該函數,但不會打印其後的函數。

任何想法爲什麼?預先感謝你,如果我犯了一個粗心的錯誤,我很抱歉,這是我的第一個問題,對於Script來說是新的。

謝謝!

+5

變量名稱不能以數字開頭。 – 2012-08-14 21:52:45

+0

'function testThree(1,2,3)'???空功能體?你想做什麼? – 2012-08-14 21:53:44

+0

如果你創建了jsfiddle,它會有所幫助。 – 2012-08-14 21:54:49

回答

0

重寫你的函數,使參數不是數字。

function testOne(var_one) 
{} 

function testTwo(var_one,var_two) 
{} 

function testThree(var_one,var_two,var_three) 
{} 

這意味着你用1功能的內部現在應該讀var_one

0

變量名稱不能以數字開頭的任何地方。

你大概意思是這樣的:

function testOne(input){ return input; } 

function testTwo(first,second){ return "" + first + " " + second; } 

function testThree(first,second,third){return "" + first + " " + second + " " + third; } 
+0

不起作用。我不使用數字我的瓦爾我只是試圖表明,我通過3參數。從文本框輸入參數是問題。 – 2012-08-15 12:44:38

相關問題