2012-08-14 76 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

相关问题