简体   繁体   English

'对象不是函数' - onclick事件

[英]'Object is not a function' - onclick event

Before I start, no I have no issues that I can find with semicolons, and I'm passing NO values to the function. 在开始之前,我没有用分号找到的问题,并且我将NO值传递给函数。

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". 当我尝试从控制台执行“login()”功能时,它工作正常,但当我单击一个HTML输入按钮来调用它时,我得到“未捕获的TypeError:对象不是一个函数”。 This is in Chrome by the way. 顺便说一句,这是在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

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的真实答案。

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

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