2017-01-30 61 views
0

提交我的HTML表單時,我總是收到此錯誤:{「code」:「MethodNotAllowedError」,「message」:「POST is not allowed」}。我的JavaScript似乎工作正常,因爲它在我把它放在腳本標記中的HTML內部時起作用,但是當我嘗試在它自己的文件中使用它時,我在表單提交中得到這個錯誤。我從Adobe Dreamweaver啓動此代碼,並使用Chrome/Safari中的實時預覽模式。這裏是我的代碼:如何從外部文件正確測試我的JavaScript代碼?

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Calculator</title> 
<link href="dopeStyles.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 

<form action="" method="post" id="numberForm"> 
<label for="enterNumber">Enter any number to multiply it by 10: </label> 
<input type="number" id="enterNumber"><br> 
<input type="submit"><br> 
<label for="result">Result: </label> 
<input type="number" id="result"> 
</form> 

</body> 
<script src="trippyJavascript.js"></script> 
</html> 

&這裏是JavaScript:

function calculate() 
{ 
    'use strict'; 
    var numInput = document.getElementById('enterNumber').value; 
    numInput*= 10; 
    document.getElementById('result').value = numInput; 
    return false; 
} 

function init() 
{ 
'use strict'; 
document.getElementByID('numberForm').onsubmit = calculate; 
} 

window.onload = init; 
+0

在在''ID'應document.getElementByID'小寫'D'。 'input'元素沒有'name'屬性。 「form」提交的默認操作不被阻止。 – guest271314

回答

0

嗨,我已經測試你code.The的錯誤是 '的document.getElementById' 這種方式不是 '的document.getElementById'。

function calculate() 
{ 
    'use strict'; 
    var numInput = document.getElementById('enterNumber').value; 
    numInput*= 10; 
    document.getElementById('result').value = numInput; 
    return false; 
} 

function init() 
{ 
'use strict'; 
document.getElementById('numberForm').onsubmit = calculate; 
} 

window.onload = init; 
+0

這工作。多麼小的一個錯誤!我必須學會更加小心。非常感謝。 –

相關問題