如何在ajax调用内的函数中传递变量?

时间:2018-06-22 17:56:43

标签: javascript jquery ajax

我声明所有包含物均由应用程序制作,请php页面正常运行。 唯一的问题是将参数obj [x] .tipologia转到未定义的函数。我无法理解原因。 可能是因为在以后打印表格后又不知道myObj [x]是什么参数而引起的问题?

function showTipo(str){
  var i=0;
$("#demo").remove();

    var obj, dbParam, xmlhttp, myObj, x,t,txt = "";
    var prova;

    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
        if (this.readyState == 4 && this.status == 200) {

         $("#secondo").before("<center><div id='demo' class='w3-row-padding w3-padding-64 w3-container'> </div></center>");
           myObj = JSON.parse(this.responseText);
          $("#demo").prepend("<div>  <table border='1' width='75%'> <tr><th width='25%'>Tipologia</th><th width='25%'>Abilitata</th><th width='25%'>Azioni</th></tr></table><table border='1' id='tab' width='75%'></div> ");

       var prova;
            for (x in myObj) {
            i=i+1;
            if(myObj[x].tipologia_abilitata != "2"){



               if(myObj[x].tipologia_abilitata == "1")
            {
              myObj[x].tipologia_abilitata = "ABILITATO";

            }
            else
            {
              myObj[x].tipologia_abilitata  = "DISABILITATO ";
            }
            prova=myObj[x].tipologia;


             txt +="<tr><td width='25%' id='cln'>" + myObj[x].tipologia + "</td> <td  width='25%'> "+myObj[x].tipologia_abilitata + "</td> <td width='25%'> <button  id='abil' class='w3-button' onClick=Abilita('./Abilta.php','luce','z')>Abilita</button> <button id='dis' class='w3-button' onClick=Disabilita(myObj[x].tipologia)'>Disabilità</button></td></tr> "; 





           }}

            txt += "</table>" 
            document.getElementById("tab").innerHTML = txt;

}




           }
$("#tab").after= " <br> <br><button   class:'w3-button'> Aggiungi Tipologia </button>    <button>Modifica</button>";
    };
    xmlhttp.open("POST", "VisualizzaCategorie.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("Nome_ente=" + str);
}


//cambio da disabilita ad abilita
function Abilita(tipo){

  var xmlhttp;
  xmlhttp = new XMLHttpRequest();
     xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    for(x in tipo){

    }
  };
      xmlhttp.open("POST", "Abilita.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("tipo=" + tipo);  

}

错误正好是:

 Uncaught ReferenceError: myObj is not defined
       at HTMLButtonElement.onclick

0 个答案:

没有答案