这是我的html和js:
function calculateFun()
{
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var c = document.getElementById('c').value;
var d = document.getElementById('d').value;
var e = document.getElementById('e').value;
var f = a*b;
document.getElementById('f').value = f;
var g = (f + (f*(d/100))).toFixed();
document.getElementById('g').value = g;
var h = ((1 -((a*c)/e))*100).toFixed();
document.getElementById('h').value = h;
}
<input type="number" id="a" onkeyup="calculateFun();" />
<input type="number" id="b" onkeyup="calculateFun();" />
<input type="number" id="c" value="100" />
<input type="number" id="d" value="50" />
<input type="number" id="e" onkeyup="calculateFun();" />
<br><br><p>******</p><br><br>
<input type="number" id="f" />
<input type="number" id="g" />
<input type="number" id="h" />
我在JSFIDDLE中尝试了以下代码:
https://jsfiddle.net/1ex3b1sa/
但它不起作用。同样在我的网站上,未调用该函数。
这很奇怪,因为我几乎可以用相同的方式调用我执行的其他功能。
我尝试更改为onclick,onkeypress或onkeydown,但看不到任何结果。
有任何想法吗?也许我有错字?也许是铬问题?
最佳答案
在JSFiddle中,您需要将JavaScript换行设置为“ No wrap-in <head>
”,否则将出现“ Uncaught ReferenceError:calculateFun未定义”错误。
确保函数在这里:
<html>
<head>
<script type="text/javascript">
function calculateFun() {
// ...
您实际上可以将函数定义保留在
onLoad
中并进行更改:function calculateFun() {
对此:
window.calculateFun = function() {
这将起作用,因为您正在将功能作为静态方法添加到浏览器的
Window
中。关于javascript - 无法使用js调用函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29514590/