-2

私の JavaScript 関数

function B_modeWindow (id,cords) {  
    loading();

    var xmlhttp;

    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            var cords = document.getElementById(cords)
            cords.innerHTML=xmlhttp.responseText;
            var xy = cords.split("x");          
            hideloading();
        }
    }

    xmlhttp.open("GET","processMapEdit.php?id="+id+"&x="+xy[0]+"&y="+xy[1],true);
    xmlhttp.send();
}

戻り値:

Uncaught ReferenceError: xy is not defined 

の:

xmlhttp.open("GET","processMapEdit.php?id="+id+"&x="+xy[0]+"&y="+xy[1],true);

xy が存在しないように見えますが、5 行上で定義されています! ここで何が問題なのですか?

4

4 に答える 4

1

xy使用している場所で使用できるようにするには、関数の外部で変数を定義する必要がありonreadystatechangeます。

これはうまくいくはずです:

function B_modeWindow (id,cords) {  
    var xy = null; // defined xy here

    loading();

    var xmlhttp;

    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            var cords = document.getElementById(cords)
            cords.innerHTML=xmlhttp.responseText;
            xy = cords.split("x");          // removed var keyword here
            hideloading();
        }
    }

    xmlhttp.open("GET","processMapEdit.php?id="+id+"&x="+xy[0]+"&y="+xy[1],true);
    xmlhttp.send();
}

デフォルトでは ajax 呼び出しは非同期であるため、スコープが固定されていても、xy変数には探しているものが含まれていない可能性があることに注意してください。その場合、実際の値を取得するには、何らかの形で ajax リクエスト内でコールバックを使用する必要があります。

于 2012-09-09T20:49:03.713 に答える
0

xy無名関数のスコープでのみ定義されますxmlhttp.onreadystatechange = function () { // valid scope of xy }。したがって、この無名関数の外からアクセスすることはできません。代わりに次を使用します。

function B_modeWindow (id,cords) {  
    loading();

    var xmlhttp;

    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    var xy = cords.split("x"); 

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            var cordsElement = document.getElementById(cords);
            cordsElement.innerHTML=xmlhttp.responseText;      
            hideloading();
        }
    }

    xmlhttp.open("GET","processMapEdit.php?id="+id+"&x="+xy[0]+"&y="+xy[1],true);
    xmlhttp.send();
}

このように、 のスコープはxy無名B_modeWindow関数ではなく関数です。

また、匿名関数の外部にアクセスできたとしてxy(これはできません)、xyAJAX 要求が完了したときにのみ定義しているため、定義されないことに注意してください。実際にそれを使用する必要があります。 AJAX 呼び出しを行います。

もう 1 つの提案は、 に異なる変数名を使用することですcords。最初にそれを使用してコンテンツを分割しx、次に別のcords変数を定義して DOM 要素を割り当てます。これは機能しますが、混乱を招く可能性があります。

于 2012-09-09T20:50:51.373 に答える
0

宣言を間違った関数に入れました。cordsまた、コールバック内の に別の名前を付けたいと思うでしょう。そうしないと、問題が発生します。

function B_modeWindow (id,cords) {
    var xy = cords.split("x");

    loading();

    var xmlhttp;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var cordsElement = document.getElementById(cords);
            cordsElement.innerHTML = xmlhttp.responseText;
            hideloading();
        }
    };

    xmlhttp.open("GET", "processMapEdit.php?id=" + id + "&x=" + xy[0] + "&y=" + xy[1], true);
    xmlhttp.send();
}
于 2012-09-09T20:50:54.320 に答える
0

ええ、 xy は定義されていますが、中括弧内で定義されています - 問題の行に到達するまでに範囲外になるか、終了を停止します。*編集: 以下のコメントを参照してください。この声明は私のものであり、明らかに間違っています*

それを修正するには、行の上に xy を定義するだけxmlhttp.onreadystatechange=function()です。またはさらに良いことに、関数の上部にあります。

于 2012-09-09T20:51:02.417 に答える