'Object is not a function' - onclick event

独自空忆成欢 提交于 2020-01-14 14:39:10

问题


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.

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.

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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/15375845/object-is-not-a-function-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!