2012-08-16 36 views
0

我知道你不能在html中有真正的變量,但我想知道這是否可能。我搜查了四周,找不到任何明確回答我的問題的東西。希望有人聽到可以指引我正確的方向。這裏就是我的想法:用js函數顯示HTML表單的結果

<!DOCTYPE html> 
<html> 
<body> 

<input type="text" name="test"> 
<input type="submit" onclick="myFunction(test)"> 
<script type="text/javascript"> 
function myFunction(test) 
{ 
alert("Welcome " + test); 
} 
</script> 

</body> 
</html> 

將這工作或類似的東西? 謝謝,山姆

+0

你想要完成什麼*確切* – Matt 2012-08-16 15:22:01

+0

要回答你的問題,不,那不行,因爲'test'沒有任何價值。 – Matt 2012-08-16 15:22:55

+0

提醒文本輸入的值,抱歉不清楚 – 2012-08-16 15:23:02

回答

3

如果你的意思是使用輸入的內容作爲一個變量:

<!DOCTYPE html> 
<html> 
<body> 

<input type="text" id="some_id" name="test"> 
<input type="submit" onclick="myFunction(document.getElementById('some_id').value)"> 
<script type="text/javascript"> 
function myFunction(test) 
{ 
alert("Welcome " + test); 
} 
</script> 

</body> 
</html> 
+0

非常感謝你 – 2012-08-16 15:30:54

0

我從你的問題理解什麼是你要訪問<input>並將其發送到function(test) 試試這個:

<input type="text" name="test" id="test"> <-- Give ID here 
<input type="submit" onclick="myFunction(test)"> <--Send same ID here 
<script type="text/javascript"> 
function myFunction(test) 
{ 
alert("Welcome " + test.value); 
} 
</script> 

</body> 
+1

你不想傳遞'document .getElementById('test')'到'myFunction()'? – Matt 2012-08-16 15:26:59

+0

@Matt這個問題並不清楚,因爲我已經說過我已經回答了我從問題中得到的理解。我認爲這個人想把'test'作爲參數發送給他的方法來獲得它的價值。 – 2012-08-16 15:31:05

+0

夠公平的。 -1刪除。 – Matt 2012-08-16 15:32:51