简体   繁体   English

JavaScript中的“未捕获的TypeError:Object不是函数”

[英]“uncaught TypeError: Object is not a function” in JavaScript

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. 我正在使用开发人员工具在谷歌浏览器中对此进行调整,并且在计算按钮的位置收到错误“未捕获的TypeError:对象不是函数”。

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. 抱歉你的问题和我的答案之间存在巨大差距:)我遇到了同样的问题并找到了原因 - 浏览器根据页面中html标签的ID属性自动填充对象。 Simple test 简单的测试

<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'的元素,重命名它或重命名您用于计算的函数。

In my case, I have 就我而言,我有

<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()时得到相同的警告,我猜它与input标记中的idtitle属性冲突,所以我只是将这个函数改为另一个名称,如signup() ,它可以解决。

I hope it helps. 我希望它有所帮助。 :) :)

I had this problem with a function I created, working perfectly with Firefox and giving this error with Chrome and Safari. 我使用我创建的功能遇到了这个问题,与Firefox完美配合,并在Chrome和Safari中出现此错误。 The solution: change the name of the function. 解决方案:更改函数的名称。 The original name was expand(target) , replaced by expand_compress(target). 原始名称为expand(target) ,替换为expand_compress(target)。 And it worked. 它奏效了。 I suppose that the function expand() is existing somewhere else than in Firefox 我认为函数expand()存在于Firefox以外的其他地方

Perhaps you need commas: 也许你需要逗号:

var val = $("#taskentry").validate({
    rules: {
        tripDate: {required: true},
        tripContact: {required: true},
        odometerStart: {required: true}     
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM