Javascript在不使用JQuery的情况下获得textarea的价值

时间:2013-04-23 01:32:38

标签: jquery textarea

我知道这应该就像执行myTextArea.value一样简单,但除非我在id上使用JQuery,否则它似乎不起作用。

所以我创建了这个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的{​​{1}}对象的方法。在这里,我尝试查看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 id包装在JQuery对象中并使用textarea确实有效,但即使.val()不是,我也希望这样做目前在屏幕上。

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

0 个答案:

没有答案