JavaScript 中的“未捕获的类型错误:对象不是函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7773775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"uncaught TypeError: Object is not a function" in JavaScript
提问by user974873
I am having trouble understanding why this is not working. I have two fields on my form and when I click a button another text field value is changed to that if the function. How can I get this to work?
我无法理解为什么这不起作用。我的表单上有两个字段,当我单击一个按钮时,另一个文本字段值更改为该函数。我怎样才能让它发挥作用?
function calculate()
{
var odometerStart = parseFloat (document.getElementById('odometerStart').value);
var odometerEnd = parseFloat(document.getElementById('odometerEnd').value);
var distance = document.getElementById('distance');
var amount = document.getElementById('amount');
distance.value = odometerEnd - odometerStart;
}
var val = $("#taskentry").validate({
rules: {
tripDate: {required: true}
tripContact: {required: true}
odometerStart: {required: true}
}
});
Odometer: Start <input type="number" name="odometer[Start]" id="odometerStart" min="0" max="999999" placeholder="0" class="required"/><br/>
Odometer: End <input type="number" name="odometer[End]" id="odometerEnd" min="0" max="999999" placeholder="999999" class="required"/><br/>
Comments <textarea name="comments" id="comments" rows="2" cols="2"></textarea><br/>
Distance <input type="text" name="distance" id="distance" value="" placeholder="0.00"/><br/>
<input type="button" name="calculate" value="Calculate" onclick="calculate()"/>
I am debuging this in Google Chrome using the developer tools and am getting the error "uncaught TypeError: Object is not a function" at the point where the calculate button is.
我正在使用开发人员工具在 Google Chrome 中对此进行调试,并在计算按钮所在的位置收到错误“未捕获的类型错误:对象不是函数”。
采纳答案by Nick Heiner
Perhaps you need commas:
也许你需要逗号:
var val = $("#taskentry").validate({
rules: {
tripDate: {required: true},
tripContact: {required: true},
odometerStart: {required: true}
}
});
回答by Cheery
Sorry for the huge gap between your question and my answer :) I just got the same problem and found the reason - browser automatically populates objects based on ID attributes of the html tags in the page. Simple test
很抱歉您的问题和我的回答之间存在巨大差距:) 我刚刚遇到了同样的问题并找到了原因 - 浏览器会根据页面中 html 标签的 ID 属性自动填充对象。简单测试
<div id='test'>Just testing</div>
<script type='text/javascript'>
alert(test.innerHTML);
</script>
In your case you had an element with id='calculate', rename it or rename the function you are using for calculations.
在您的情况下,您有一个带有 id='calculate' 的元素,请重命名它或重命名您用于计算的函数。
回答by JLHwung
In my case, I have
就我而言,我有
<input type="button" class="button" id="register" title="register" value="Sign Up!" onclick="register()" />
<input type="button" class="button" id="register" title="register" value="Sign Up!" onclick="register()" />
and get the same warning when I call the javascript function register()
, I guess it is conflicted with id
or title
attributes in the input
tag, so I just alter this function into another name such as signup()
and it works around.
并在我调用 javascript 函数时收到相同的警告register()
,我猜它与标签中的id
或title
属性冲突input
,所以我只是将此函数更改为另一个名称,例如signup()
,它可以解决。
I hope it helps. :)
我希望它有帮助。:)
回答by Armand Arapian
I had this problem with a function I created, working perfectly with Firefox and giving this error with Chrome and Safari. The solution: change the name of the function. The original name was expand(target)
, replaced by expand_compress(target). And it worked. I suppose that the function expand()
is existing somewhere else than in Firefox
我创建的一个函数遇到了这个问题,与 Firefox 完美配合,并在 Chrome 和 Safari 中出现了这个错误。解决办法:改函数名。原来的名字是expand(target)
,由 expand_compress(target) 代替。它奏效了。我想该功能expand()
存在于 Firefox 之外的其他地方