Nella funzione xmlhttp.onreadystatechange, come posso passare il nome dell'ID che voglio modificare?

StackOverflow https://stackoverflow.com/questions/1425358

  •  07-07-2019
  •  | 
  •  

Domanda

Ecco il mio codice. Vedi la riga commentata. Quando l'id dell'elemento (che è un intervallo) è codificato, funziona. Quando l'id viene creato concatenando le variabili passate in stateChanged, non funziona. Non sono autorizzato a passare le variabili a stateChanged? Cosa c'è che non va?

function multiplePassportPoints(id, counter)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="addmorepoints.php"; 
url=url+"?id="+id+"&c="+counter;
url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged(id,counter);
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged(id, counter) 
{
  if (xmlhttp.readyState==4)
  {
    //THIS WORKS (assuming id is 99 and counter is 5:
    //document.getElementById("99_5").innerHTML += xmlhttp.responseText; 

    //BUT I NEED IT TO WORK LIKE THIS:
    document.getElementById(studentID+"_"+counter).innerHTML += xmlhttp.responseText; 
  }
}

Grazie!

È stato utile?

Soluzione

Puoi cambiare il codice in questo

xmlhttp.onreadystatechange = function () {
        stateChanged(id,counter);
    };    

Altri suggerimenti

 <script type="text/javascript">
 var i=1;
 function validate(str)
 { 
 xmlHttp=GetXmlHttpObject()
 var url="checkvalidate.php"
 url=url+"?User9="+str
 xmlHttp.onreadystatechange=stateChanged19 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)
 }
function stateChanged19() 
 {  

 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { 

  document.getElementById("valid"+i)
  .innerHTML=xmlHttp.responseText 
  i=i+1;
   } }
 </script>

Un metodo migliore che può essere chiamato più volte prima del termine della chiamata precedente. Notare che se si chiamano multiplePassportPoints due volte il valore precedente di xmlhttp verrà sovrascritto. Ci sono due risultati:
1- tutto funziona bene quando non si verifica alcuna concorrenza (possibilità molto alta),
2- la prima chiamata non avviene mai (possibilità molto bassa ma accadrà di tanto in tanto e sarà molto difficile individuare e riprodurre)

Ma il codice seguente usa la variabile locale e potrebbe (non testato) essere sicuro da chiamare ancora e ancora.

function multiplePassportPoints(id, counter) {
  var xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  }

  var url="addmorepoints.php"; 
  url=url+"?id="+id+"&c="+counter;
  url=url+"&sid="+Math.random();

  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      stateChanged(id, data, xmlhttp.responseText);
    }
  };

  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function stateChanged(id, counter,text) 
{
    document.getElementById(id+"_"+counter).innerHTML += text; 
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top