Onkeyup non funziona? Forse è un cattivo codice Ajax?
-
28-10-2019 - |
Domanda
Sto solo imparando AJAX e il seguente file HTML non funziona. Ogni volta che rilascio una chiave, Firebug dice "Login non è una funzione". Non riesco a capire perché questo sia. Qualsiasi aiuto è apprezzato.
<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>
Soluzione
onKeyUp="login();"
sembra trovare la tua forma (anche denominata login
). Rinominare la forma a qualcosa di diverso (ad esempio login_form
) per me va bene.
Altri suggerimenti
Prova a usare un nome di forma diverso. Hai un modulo chiamato "Login" e una funzione JavaScript chiamata Login. Penso che Firebug sia confuso tra i due ...
Inoltre, metti la variabile AJAX al di fuori della funzione, .ie
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;
}
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow