开发者

onKeyUp not working? maybe it's bad ajax code?

开发者 https://www.devze.com 2023-04-04 05:18 出处:网络
I am just learning AJAX, and the following HTML file is not working. Every time I release a key, firebug says \"login is not a function\". I can\'t figure out why this is. Any help is appreciated.

I am just learning AJAX, and the following HTML file is not working. Every time I release a key, firebug says "login is not a function". I can't figure out why this is. Any help is appreciated.

开发者_JAVA技巧<html>
<head>
<title>Ajax</title>
<script type="text/javascript">
function makeAjax() {
    var ajax;
    try{
        ajax = new XMLHttpRequest();
    }
    catch (e) {
        try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser is either OLD or BAD! UPDATE!");
                return false;
            }
        }
    }
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            document.getElementById("btag").innerHTML = ajax.responseText;
        }
    }
}
function login() {
    ajax.open("GET", "ajax.php", true);
    ajax.send(null); 
}
</script>
</head>
<body onLoad="makeAjax();">
<form name="login">
Username: <input type="text" onKeyUp="login();" />
<br /><br />
Password: <input type="password" onKeyUp="login();" />
<b id="btag"></b>
</form>
</body>
</html>


onKeyUp="login();" seems to be finding your form (also named login). Renaming the form to something different (e.g. login_form) works for me.


Try using a different form name. You have a form named 'login' and a Javascript function called Login. I think firebug is confused between the two...

Also, put the Ajax variable outside the function, .i.e.

var ajax;
function makeAjax() {

    try{
        ajax = new XMLHttpRequest();
    }
    catch (e) {
        try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                ajax = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser is either OLD or BAD! UPDATE!");
                return false;
            }
        }
    }
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            document.getElementById("btag").innerHTML = ajax.responseText;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消