2017-01-30 101 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

这工作。多么小的一个错误!我必须学会更加小心。非常感谢。 –

相关问题