0

我知道这应该和做一样简单myTextArea.value,但除非我在id.

所以我创建了这个 Javascript 对象,并在其上分配了一个名为assignmentQuestionTextArea. 这是我这样做的完整代码:

TextQuestionAssignmentTakingPage.prototype.makeContent = function (question) {
    var questionWrapper = StageAssignmentTakingPage.prototype.makeContent.call(this, question);

    var answerDiv = document.createElement("div");
    answerDiv.setAttribute("id", "assignmentTakingQuestionAnswerWrapper");
    var textArea = document.createElement("textarea");
    textArea.setAttribute("id", "assignmentTakingTextQuestionTextArea");
    textArea.innerHTML = this.answerSupplied;
    this.assignmentQuestionTextArea = textArea;
    answerDiv.innerHTML = textArea.outerHTML;

    questionWrapper.innerHTML += answerDiv.outerHTML;

    return questionWrapper.outerHTML;
};

就这样。早就这么叫了。然后,当我稍后单击某个按钮时,我调用了该TextQuestionAssignmentTakingPage对象的一个​​方法,称为hasBeenChanged. 在这里,我尝试查看 的值,this.assignmentQuestionTextArea看看它是否与我保存的不同。保存的值位于名为 的属性中answerSupplied

TextQuestionAssignmentTakingPage.prototype.hasBeenChanged = function () {
    alert($('#assignmentTakingTextQuestionTextArea').val() + " - " + this.assignmentQuestionTextArea.value + " - " + this.assignmentQuestionTextArea.innerHTML);
    if (this.answerSupplied === null) { // just need to check if an answer exists
        var patt = /^\s*$/; // pattern for any number of spaces and nothing else
        var result = patt.test($(this.assignmentQuestionTextArea).val());
        if (result == true) {
            return false;
        } else {
            return true;
        }
    } else { // need to look at what the answer was and see if it has changed
        if ($(this.assignmentQuestionTextArea).val() === this.answerSupplied) {
            return false;
        } else {
            return true;
        }
    }
};

问题是,第一次这样做,this.assignmentQuestionTextArea.value似乎总是一个空字符串。是的,它确实可以通过将 的 包装idtextareaJQuery 对象中并使用.val()来工作,但我真的很希望它能够工作,即使this.assignmentQuestionTextArea当前不在屏幕上。

为什么它不能给我最新的价值?

4

0 回答 0