javascript “对象不是函数” - onclick 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15375845/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:35:09  来源:igfitidea点击:

'Object is not a function' - onclick event

javascriptfunctionsyntax-error

提问by Ricky Yoder

Before I start, no I have no issues that I can find with semicolons, and I'm passing NO values to the function.

在我开始之前,不,我没有用分号找到的问题,而且我没有向函数传递任何值。

When I try to do function "login()" from the console, it works just fine, but when I click an HTML input button to call it, I get "Uncaught TypeError: object is not a function". This is in Chrome by the way.

当我尝试从控制台执行函数“login()”时,它工作得很好,但是当我单击 HTML 输入按钮来调用它时,我得到“Uncaught TypeError: object is not a function”。顺便说一下,这是在 Chrome 中。

var aj=new XMLHttpRequest();
var loginf=document.forms["login"];
var regf=document.forms["reg"];

function login(){
var errors=document.getElementById("login_err");
var errs=[];
var uname=loginf['uname'];
var pass=loginf['pass'];
var unameVal=uname.value;
var passVal=pass.value;

if(unameVal.length<4 && unameVal.length>0){
    errs.push("Username too short. Try again please.");
}
else if(unameVal.length>18){
    errs.push(innerHTML="Username is too long. Try again please.");
}
else if(unameVal.length==0){
    errs.push("Please enter a username.");
}

if(passVal.length<8){
    errs.push("Password too short. Try again, please.");
}
else if(passVal.length>30){
    errs.push("Password is too long. Try again please.");
}

if(errs.length==0){
    aj.onreadystatechange=function(){
        if(aj.readyState>=4 && aj.status==200){
            if(aj.responseText=="suc"){
                window.location="/new.php";
            }
        }
    }
    aj.open("POST","/inc/php/core.php",false);
    aj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aj.send("login=true&data="+data);
}
else{
    errors.innerHTML="<ul>";

    for(var x=0;x<errs.length;x++){
        errors.innerHTML+="<li>"+errs[x]+"</li>";
    }

    errors.innerHTML+="</ul>";
}
}

function reg(){

}

And then the input button..

然后输入键..

<input type="button" class="submit" value="Log in" style="width:25%" onclick="login();"/>

I see nothing wrong with the code.

我看代码没有任何问题。

Thank you in advance if you can find Waldo in this code.

如果您能在此代码中找到 Waldo,请提前致谢。

jsFiddle HERE: http://jsfiddle.net/6sa7M/2

jsFiddle在这里:http: //jsfiddle.net/6sa7M/2

回答by Ricky Yoder

It turns out that the function name "login" was both a reference to the form and function.

事实证明,函数名“login”既是对表单的引用,也是对函数的引用。

I changed "login()" to "loginUser()", and typed "login" into the console, and the form was returned, so they were indeed conflicting with each other.

我把“login()”改成了“loginUser()”,在控制台输入“login”,返回了表单,所以确实是互相冲突了。

Thank you again for all the help and special thanks to Marc for the true answer.

再次感谢您的所有帮助,并特别感谢 Marc 的真实回答。