Web 开发新手,遇到错误 (jsfiddle)
New to web developing, got an error (jsfiddle)
我是 web 开发的初学者,我想简单地在在线工具 jsfiddle 中做一个机会计算器。我收到错误:"Shell form does not validate" 之后出现了一些奇怪的错误。这是我的 HTML 代码:
<body>
<form method="post">The chance of succes:
<input type="number" name="chance">
<br>How many tries:
<input type="number" name="tries">
<br>
<input type="submit" value="Calculate!">
<br>
</form>
</body>
我的 javascript 代码是:
function calculate(chance, tries) {
console.log(chance / tries * 100);
}
正如我所说,我是新手所以请尝试逐步解释。
你有一些问题。一是您的表单正试图将自己提交给自己。另一个是您的计算函数从未被调用。
您的函数参数也从未被调用。让我们将输入更改为具有 ID 而不是名称。例如:
<input type="number" id="chance">
这样可以更轻松地在单击按钮时从输入中获取值。我将输入设为按钮而不是提交,只是为了确保您的表单数据不会发送到任何地方。
<input type="button" onclick="return calculate(getElementById('chance').value, getElementById('tries').value)" value="Calculate!">
这是一个 jsfiddle,其中包含适合您的可能解决方案。 http://jsfiddle.net/0dmkxcab/
我是 web 开发的初学者,我想简单地在在线工具 jsfiddle 中做一个机会计算器。我收到错误:"Shell form does not validate" 之后出现了一些奇怪的错误。这是我的 HTML 代码:
<body>
<form method="post">The chance of succes:
<input type="number" name="chance">
<br>How many tries:
<input type="number" name="tries">
<br>
<input type="submit" value="Calculate!">
<br>
</form>
</body>
我的 javascript 代码是:
function calculate(chance, tries) {
console.log(chance / tries * 100);
}
正如我所说,我是新手所以请尝试逐步解释。
你有一些问题。一是您的表单正试图将自己提交给自己。另一个是您的计算函数从未被调用。
您的函数参数也从未被调用。让我们将输入更改为具有 ID 而不是名称。例如:
<input type="number" id="chance">
这样可以更轻松地在单击按钮时从输入中获取值。我将输入设为按钮而不是提交,只是为了确保您的表单数据不会发送到任何地方。
<input type="button" onclick="return calculate(getElementById('chance').value, getElementById('tries').value)" value="Calculate!">
这是一个 jsfiddle,其中包含适合您的可能解决方案。 http://jsfiddle.net/0dmkxcab/