2

我尝试使用此方法将变量传递给下一个函数:

function a(form, ctr) {
var a = form;
var b = ctr;
b(ctr);
} 

function b(ctr) {
var b = ctr;
}

确切的代码要复杂得多,因为我正在使用带有 ajax 的 POST 方法:函数 a 在单击时开始并使用 form 和 ctr 参数 - 然后它转到只需要 ctr 的函数 b - 但是这种传递方法变量不起作用。有更好的解决方案吗?

function updateQuestion(form, ctr) { 
console.log("Called updateQuestion");
var str1 = "toggleDiv";
var str2 = ctr;
var id = str1.concat(str2);
var divVar = document.getElementById(id);
console.log(divVar);
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(XMLHttpRequestObject) {
  console.log("XMLHttpRequestObject = TRUE");

var myVar = form.create_mcq_question.value;
  console.log("MyVar = " + myVar);
    var formQuestion = document.getElementById("formQuestion");
    console.log("1");
    XMLHttpRequestObject.open("POST","hiddent",true); 
        console.log("2");
    XMLHttpRequestObject.onreadystatechange  = handleServerResponse;
        console.log("3");
    XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        console.log("4" + document.getElementById("create_mcq_question").value);

        var mcqid;
        var mcqQuestion;
        var mcqAnswerCorrect;
        var mcqAnswerWrong1;
        var mcqAnswerWrong2;
        var mcqAnswerWrong3;
        var mcqExplanation;

        var error = 0;

        if (form.create_mcq_question.value == "" || form.create_mcq_question.value == null  ) {
        error = 1;
        }
        else {
        mcqQuestion = form.create_mcq_question.value
        }
        if (form.create_mcq_answer_correct.value == "" || form.create_mcq_answer_correct.value == null) {
        mcqAnswerCorrect = "";
        error = 1;
        }
        else {
        mcqAnswerCorrect = form.create_mcq_answer_correct.value
        }
        if ((form.create_mcq_answer_wrong1.value == "" || form.create_mcq_answer_wrong1.value == null) && (form.create_mcq_answer_wrong2.value == "" || form.create_mcq_answer_wrong2.value == null) && (form.create_mcq_answer_wrong3.value == "" || form.create_mcq_answer_wrong3.value == null)) {
        error = 1;
        }

        if (form.create_mcq_answer_wrong1.value == "" || form.create_mcq_answer_wrong1.value == null) {
        mcqAnswerWrong1 = "";
        }
        else {
        mcqAnswerWrong1 = form.create_mcq_answer_wrong1.value
        }
                if (form.create_mcq_answer_wrong2.value == "" || form.create_mcq_answer_wrong2.value == null) {
        mcqAnswerWrong2 = "";
        }
        else {
        mcqAnswerWrong2 = form.create_mcq_answer_wrong2.value
        }
                if (form.create_mcq_answer_wrong3.value == "" || form.create_mcq_answer_wrong3.value == null) {
        mcqAnswerWrong3 = "";
        }
        else {
        mcqAnswerWrong3 = form.create_mcq_answer_wrong3.value
        }

                if (form.create_mcq_explanation.value == "" || form.create_mcq_explanation.value == null) {
        mcqExplanation = "";
        }
        else {
        mcqExplanation = form.create_mcq_explanation.value
        }
        if (error == 0) {
    XMLHttpRequestObject.send("create_mcq_question=" + mcqQuestion +
                              "&create_mcq_correct_answer=" + mcqAnswerCorrect + 
                              "&create_mcq_wrong_answer1=" + mcqAnswerWrong1 + 
                              "&create_mcq_answer_wrong2=" + mcqAnswerWrong2 + 
                              "&create_mcq_answer_wrong3=" + mcqAnswerWrong3 + 
                              "&create_mcq_explanation=" + mcqExplanation +
                              "&mcqid=" + mcqid ); 
                                console.log("5");
                                handleServerResponse(ctr);
                                }
                                else {
                                document.getElementById("divVar").innerHTML="Cannot update question - please ensure all required fields are filled!";
                                }
  }


}

function handleServerResponse(ctr) {
var str1 = "toggleDiv";
var str2 = ctr;
var id = str1.concat(str2);
var divVar = document.getElementById(id);
console.log("Handle server response called");
if (XMLHttpRequestObject.readyState == 1) {
console.log("Loading");
 document.getElementById(divVar).innerHTML="<img src=\"hidden">";
}

   if (XMLHttpRequestObject.readyState == 4) {
   console.log("4");
     if(XMLHttpRequestObject.status == 200) {
       document.getElementById(divVar).innerHTML=XMLHttpRequestObject.responseText; //Update the HTML Form element
       console.log("divVar found");
       console.log(divVar); 
     }
     else {
       document.getElementById(divVar).innerHTML="There was a problem updating your question - please try again!"; //Update the HTML Form element 
       console.log("divVar not found");
       console.log(divVar); 
     }
   }
}

以及启动整个过程的按钮:

<input type="button" value="Update My Question!" onclick="updateQuestion(this.form,<?php echo" $ctr"; ?>)">

Firebug 显示第一个函数工作,并调用第二个函数,它没有得到变量:

[02:21:42.106] Called updateQuestion
[02:21:42.106] [object HTMLDivElement]
[02:21:42.106] XMLHttpRequestObject = TRUE
[02:21:42.106] MyVar = Gram- bacteria are stained purple with gram staining, while gram+ bacteria are stained pink.
[02:21:42.106] 1
[02:21:42.107] 2
[02:21:42.107] 3
[02:21:42.107] 4Gram- bacteria are stained purple with gram staining, while gram+ bacteria are stained pink.
[02:21:42.108] Handle server response called
[02:21:42.108] Loading
[02:21:42.108] 5
[02:21:42.108] Handle server response called
[02:21:42.109] Loading
[02:21:42.649] Empty string passed to getElementById(). @ hidden
[02:21:42.649] TypeError: document.getElementById(...) is null @ hidden
[02:21:42.647] Handle server response called
[02:21:42.648] 4
4

2 回答 2

2

在您的函数范围内已经有一个名为“b”的变量。这就是你得到错误的原因。如果它们是全局函数,您可以使用:

function a(form, ctr) {
    var a = form;
    var b = ctr;
    window.b(ctr);
} 

function b(ctr) {
    var b = ctr;
}
于 2012-12-27T02:18:52.490 回答
2

以下内容真的没有意义:

var b = ctr;
b(ctr);

在这里,变量b被视为函数并被调用,并且您将对其自身的引用作为参数传递。这真的是我们的意图吗?

编辑:现在有了附加信息,问题很容易解释。如果您查看您的日志,您会注意到 handleServerResponse 被多次调用。第一次“ctr”按预期传递。

问题在这里:

XMLHttpRequestObject.onreadystatechange  = handleServerResponse;

这会设置一个回调,回调当然会在没有“ctr”的情况下调用你的函数,这就是你的代码运行的原因。您可以使用匿名函数,以便使用闭包:

XMLHttpRequestObject.onreadystatechange = function() { handleServerResponse(ctr); };

您可能想阅读 Crockford 的 JavaScript:The Good Parts,我认为这将有助于显着改进您的 JS 编码。

于 2012-12-27T02:14:50.277 回答