2014-11-16 44 views
0

請幫助我,我對所有這些都比較陌生,我只是想知道如何才能將這些數據提交給我的簡單JavaScript文件,然後執行一些命令。那就是:向我展示的代碼,而不是實際運行它

<html> 
<head> 
    <center><h1>Test-Page</h1></center> 
    <title>Hardware Compare</title> 
</head> 
<body> 
<script src="text/javascript"> 
function updateInput() { 
var userIDInput = document.getElementById('userID'); 
var submittedInput = userIDInput.value; 
document.getElementById('testingID').innerHTML='submittedInput' 
} 
</script> 
    <div class="testing"> 
    <h2 id="testingID">Sign In</h2> 
    <form name="input" action="login.js" method="POST"> 
     Username: <input type="text" name="userID" /> 
     Password: <input type="password" name="password" /> 
     <input type="submit" value="Submit" name="sub" /> 
    </form> 
    </div> 
</body> 

,這裏是JavaScript代碼:

function updateInput() { 
var userIDInput = document.getElementsByName("UserID")[0].value; 
var submittedInput = userIDInput.value; 
document.getElementById('testingID').innerHTML=submittedInput 

}

的roblem是,在提交時,網頁瀏覽器顯示實際的代碼,而不是運行它:/這讓我困擾了一段時間,我似乎無法修復它。這可能是一些常見的noob錯誤,但我真的很感謝一些幫助,歡呼!

回答

2

表單以PHP,Ruby,NodeJS,Python等方式提交給服務器端腳本。您無法提交到您嘗試使用的其他JavaScript文件。

但看起來你需要的形式onsubmit事件。在這種情況下,HTML將是:

<form name="input" onsubmit="return updateInput();" method="POST"> 
    Username: <input type="text" name="userID" /> 
    Password: <input type="password" name="password" /> 
    <input type="submit" value="Submit" name="sub" /> 
</form> 

然後updateInput功能會處理這個事件,做任何你想做的事情:

function updateInput() { 
    var userIDInput = document.getElementsByName("userID")[0]; 
    var submittedInput = userIDInput.value; 
    document.getElementById('testingID').innerHTML = submittedInput; 
    return false; 
} 

另外請注意,你必須在updateInput功能兩個錯誤。輸入名稱是userID,您需要從userIDInput刪除.value

+0

太棒了!謝謝:) – HazzaMcJazza

+0

不客氣,很高興幫助! – dfsq

相關問題