jQuery小部件方法未检测到

时间:2019-02-14 03:35:44

标签: javascript jquery widget

在我的一生中,我看不到复制代码之间的巨大区别。根据父窗口小部件,子窗口小部件中的_getMiscData方法显然不存在。有人可以解释吗?

在使用保留字的情况下,我尝试了不同的方法名称。我尝试了私有和公共方法,结果相同。

$(function() {
    $.widget("custom.textquestion", $.custom.base, {
        _create: function() {
            this.element.data("_getControlValue", this.getControlValue());
        },
        getControlValue: function() {
            this.element.attr("usersanswer", this.getResponseJSON());
        },
        _getAnswerSelection: function(answer) {
            var qname = this._getQuestionName();
            var type = this.getType();
            return '<div class="radio span6"><label><input type="text" class="answerfield" name="optradio-' + qname + '" value="' + answer.answertext + '"></label></div>'
        },
        _getAnswersDiv: function(answers) {
            // DIV is required for wrapping around list of answers
            // so that they can be added all at once.
            var answerdivs = '<div class="answerlist">'
            for (k = 0; k < answers.length; k++) {
                answerdivs += this._getAnswerSelection(answers[k]);
            }
            answerdivs += '</div>';

            return answerdivs;
        },
        _getPopulatedEditor: function(answers) {
            var editanswerdiv = "";
            for (p = 0; p < answers.length; p++) {
                editanswerdiv += '<label>Default</label><input type="text" onblur="savequestiondata()" identifier="' + answers[p].answerid + '" name="editanswer' + this._getQuestionName(this.options.questioncontrol) + '" size="20" value="' + answers[p].answertext + '"/><br/>'
            }
            return editanswerdiv;
        },
        _getAnswersJSON: function(div) {
            var answers = [];
            $(div).find("input[name='editanswer" + this._getQuestionName(this.options.questioncontrol) + "']").each(function(i, u) {
                var answer = {
                    answerid: $(this).attr('identifier'),
                    answertext: $(this).val(),
                    answerorder: 0,
                    rating: 0
                };

                answers.push(answer);
            });
            return answers;
        },
        _setAnswerJSON: function(answer) {
            answer = answer.replace(/['"]+/g, '');
            this.options.questioncontrol.find("input[name='optradio-" + this._getQuestionName() + "']").val(answer);
            return true;
        },
        getResponseJSON: function() {
            qname = this._getQuestionName();
            console.log("The control name is " + qname);
            var answer = this.options.questioncontrol.find("input[name='optradio-" + qname + "']").val();
            console.log("The answer is " + answer);
            return answer;

        },
        _refresh: function() {
            qname = this._getQuestionName(this.options.questioncontrol);

            // Create the divs for the answer block, then
            // append to this question as the list of answers.
            var answers = this._getStoredData().data.answers;
            var answerhtml = this._getAnswersDiv(answers);
            this.options.questioncontrol.find(".answer-list").html("");
            this.options.questioncontrol.find(".answer-list").append(answerhtml);

            // Get the explanation text and add to control.
            var explanation = this._getStoredData().data.explanation;
            this.options.questioncontrol.find(".explanation").text(explanation);

            // Populate the editor controls with the answers that
            // are already stored - ready for editing.
            var editanswerhtml = this._getPopulatedEditor(answers);
            this.options.questioncontrol.find(".editanswers").html("");
            this.options.questioncontrol.find(".editanswers").append(editanswerhtml);
            this.options.questioncontrol.find(".notes").text(explanation);
        },
        _getQuestionText: function(div) {
            var qtext;
            $(div).find("input[name='questiontextedit']").each(function() {
                qtext = $(this).val();
            });
            return qtext;
        },
        _getMiscData: function() {
            var info = [this.options.questioncontrol.find(".email-mask").val()];
            return info;
        },
        _setOtherData: function(info) {
            if (info.emailmask == "true")
                this.options.questioncontrol.find(".email-mask").attr("checked", "checked");
            else
                this.options.questioncontrol.find(".email-mask").attr("checked", "");
        },
        _getExplanationText: function(div) {
            var etext = $(div).find(".notes").val();
            return etext;
        }
    });
});

$(function() {
    $.widget("custom.base", {
        // default options
        options: {
            questioncontrol: this,
            questiondata: null,
            _storedData: [],
        },

        _create: function() {

        },

        _refresh: function() {

        },

        getEditedAnswers: function(div) {
            // Get the set of answers that were edited.
            if (!div)
                div = this.options.questioncontrol;

            var answersresult = this._getAnswersJSON(div);
            var question = this._getQuestionText(div);
            var typename = this.getType();
            var qname = this._getQuestionName(this.options.questioncontrol);
            var pagenumber = this._getStoredData(qname).data.pagenumber;
            var order = this._getStoredData(qname).data.order;
            var explanation = this._getExplanationText(div);
            var otherdata = this._getMiscData();
            var result = {
                title: question,
                type: typename,
                pageid: pagenumber,
                qname: qname,
                answers: answersresult,
                questionorder: order,
                explanation: explanation,
                otherdata: otherdata
            };

            return result;
        },
        getType: function() {
            return $(this.options.questioncontrol).attr('id');
        },
        setData: function(questiondata) {
            // Set the title for the question. I.e, the text
            // for the question.

            this.options.questioncontrol.find(".questiontitle").text(questiondata.title);
            this.options.questioncontrol.find("input[name='questiontextedit']").val(questiondata.title);
            this.options.questioncontrol.find(".notes").text(questiondata.explanation);
            // Store the data into datastore.
            var stored = 0;
            if (this._getStoredData(questiondata.qname) == null) {
                this.options._storedData.push({
                    qname: questiondata.qname,
                    data: questiondata
                });

            } else {
                var datastored = this._getStoredData(questiondata.qname);
                datastored.data = questiondata;
            }

            if (questiondata.otherdata)
                this._setOtherData(questiondata.otherdata);

            this._refresh();
        },

        setAnswer: function(answer) {
            this._setAnswerJSON(answer);
        },




        _getQuestionName: function() {
            return this.options.questioncontrol.find('.questionname').val();
        },

        _getStoredData: function() {
            var qname = this._getQuestionName(this.options.questioncontrol);
            for (p = 0; p < this.options._storedData.length; p++) {
                if (this.options._storedData[p].qname == qname)
                    return this.options._storedData[p];
            }
            return null;
        },


    });
});

custom.base小部件不应返回“ this.getMiscData不是函数”

0 个答案:

没有答案