【问题标题】:How to add the score, according to the value with survey.js?如何根据survey.js的值添加分数?
【发布时间】:2020-06-09 15:33:35
【问题描述】:

我正在做一个测验,要求根据选择的值,一个值(分数)。在测验结束时,根据所选选项添加分数。 这是我的代码:Html. (图书馆。survey.ko.js)

<div id="surveyElement"></div>
<div id="result"></div>

Js

var json = {
     "title": "Quiz",
     "description": "Prueba de Quiz",
     "pages": [
      {
       "name": "page1",
       "elements": [
    {
     "type": "radiogroup",
     "name": "LifeStyleQ11E1",
     "title": "Do you smoke?",
     "isRequired": true,
     "choices": [
      {
       "value": "SmokeYes",
       "text": "Yes",
       "score": 5
      },
      {
       "value": "SmokeNo",
       "text": "No",
       "score": 4
      }
     ]
    }
   ],
       "title": "Intro",
       "maxTimeToFinish": 2
      },
      {
       "name": "page2",
       "elements": [
        {
         "type": "radiogroup",
     "name": "LifeStyleQ10E1",
     "title": "Do you suffer from back pain?",
     "isRequired": true,
     "choices": [
      {
       "value": "BackPainYes",
       "text": "Yes",
       "score": -4
      },
      {
       "value": "BackPainNo",
       "text": "No",
       "score": 5 //Este score se tendria que sumar en cada pregunta.
      }
       ],
       "title": "Intro"
      }};
    var survey = new Survey.Model(json);

survey.onComplete.add(function(result) {
    document.querySelector('#result').innerHTML = "result: " + JSON.stringify(result.data); 
});

我想要做的是,当他们点击一个选项时,它会添加到前一个选项中;直到测验结束。我看到 JSON 结果只显示“值”而不显示“分数”元素。你有什么解决办法吗?

【问题讨论】:

    标签: javascript arrays json surveyjs


    【解决方案1】:

    我相信来自 SurveyJS 库源代码的这个单元测试准确地展示了您的场景 - https://github.com/surveyjs/survey-library/blob/master/tests/surveytests.ts#L7266-L7594

    QUnit.test(
      "custom fields in getPlainData - https://surveyjs.answerdesk.io/ticket/details/T1778",
      function (assert) {
        Serializer.addProperty("question", {
          name: "score:number",
        });
    
        Serializer.addProperty("itemvalue", {
          name: "score:number",
        });
    
        var q = new QuestionImagePickerModel(null);
    
        var survey = new SurveyModel({
          questions: [
            {
              type: "rating",
              name: "question11",
              score: 11,
            },
            {
              type: "dropdown",
              name: "question12",
              score: 12,
              choices: [
                {
                  value: "item1",
                  score: 1,
                },
                {
                  value: "item2",
                  score: 2,
                },
                {
                  value: "item3",
                  score: 3,
                },
              ],
            },
            {
              type: "text",
              name: "question13",
              score: 13,
            },
            {
              type: "checkbox",
              name: "question14",
              score: 14,
              choices: [
                {
                  value: "item1",
                  score: 1,
                },
                {
                  value: "item2",
                  score: 2,
                },
                {
                  value: "item3",
                  score: 3,
                },
              ],
            },
            {
              type: "radiogroup",
              name: "question1",
              score: 1,
              choices: [
                {
                  value: "item1",
                  score: 1,
                },
                {
                  value: "item2",
                  score: 2,
                },
                {
                  value: "item3",
                  score: 3,
                },
              ],
            },
            {
              type: "comment",
              name: "question2",
              score: 2,
            },
            {
              type: "imagepicker",
              name: "question3",
              score: 3,
              choices: [
                {
                  value: "lion",
                  score: 1,
                  imageLink:
                    "https://surveyjs.io/Content/Images/examples/image-picker/lion.jpg",
                },
                {
                  value: "giraffe",
                  text: "giraffe22",
                  score: 2,
                  imageLink:
                    "https://surveyjs.io/Content/Images/examples/image-picker/giraffe.jpg",
                },
                {
                  value: "panda",
                  score: 3,
                  imageLink:
                    "https://surveyjs.io/Content/Images/examples/image-picker/panda.jpg",
                },
                {
                  value: "camel",
                  score: 4,
                  imageLink:
                    "https://surveyjs.io/Content/Images/examples/image-picker/camel.jpg",
                },
              ],
            },
            {
              type: "file",
              name: "question4",
              score: 4,
            },
            {
              type: "multipletext",
              name: "question5",
              score: 5,
              items: [
                {
                  name: "text1",
                },
                {
                  name: "text2",
                },
              ],
            },
            {
              type: "matrixdropdown",
              name: "question9",
              score: 9,
              columns: [
                {
                  name: "Column 1",
                },
                {
                  name: "Column 2",
                },
                {
                  name: "Column 3",
                },
              ],
              choices: [
                {
                  value: 1,
                  score: 1,
                },
                {
                  value: 2,
                  score: 2,
                },
                {
                  value: 3,
                  score: 3,
                },
                {
                  value: 4,
                  score: 4,
                },
                {
                  value: 5,
                  score: 5,
                },
              ],
              rows: ["Row 1", "Row 2"],
            },
            {
              type: "matrix",
              name: "question10",
              score: 10,
              columns: [
                {
                  value: "Column 1",
                  score: 1,
                },
                {
                  value: "Column 2",
                  score: 2,
                },
                {
                  value: "Column 3",
                  score: 3,
                },
              ],
              rows: ["Row 1", "Row 2"],
            },
            {
              type: "panel",
              name: "panel1",
              elements: [
                {
                  type: "radiogroup",
                  name: "question6",
                  score: 6,
                  choices: [
                    {
                      value: "item1",
                      score: 1,
                    },
                    {
                      value: "item2",
                      score: 2,
                    },
                    {
                      value: "item3",
                      score: 3,
                    },
                  ],
                },
                {
                  type: "file",
                  name: "question7",
                  score: 7,
                },
              ],
            },
            {
              type: "paneldynamic",
              name: "question8",
              score: 8,
              templateElements: [
                {
                  type: "text",
                  score: 21,
                  name: "question21",
                },
              ],
            },
          ],
        });
        survey.data = {
          question11: 3,
          question12: "item2",
          question13: "some text 13",
          question14: ["item2", "item3"],
          question1: "item1",
          question2: "comment2",
          question3: "giraffe",
          question4: [
            {
              name: "favicon.ico",
              type: "image/x-icon",
              content: "data:image/x-icon;base64,A=",
            },
          ],
          question5: { text1: "a", text2: "b" },
          question9: {
            "Row 1": { "Column 1": 1, "Column 2": 2, "Column 3": 3 },
            "Row 2": { "Column 1": 4, "Column 2": 5, "Column 3": 4 },
          },
          question10: { "Row 1": "Column 1", "Row 2": "Column 2" },
          question8: [
            { question21: "Panel dynamic content 1" },
            { question21: "Panel dynamic content 2" },
          ],
        };
    
        var plainData = survey.getPlainData({
          calculations: [{ propertyName: "score" }],
        });
        assert.equal(plainData.length, 12, "all questions are present");
        assert.deepEqual(plainData[0].value, 3);
        assert.equal(plainData[0].score, 11);
        assert.equal(plainData[0].isNode, false);
        assert.deepEqual(plainData[1].value, "item2");
        assert.equal(plainData[1].score, 12);
        assert.equal(plainData[1].isNode, true);
        assert.equal(plainData[1].data[0].score, 2);
        assert.deepEqual(plainData[2].value, "some text 13");
        assert.equal(plainData[2].score, 13);
        assert.deepEqual(plainData[3].value, ["item2", "item3"]);
        assert.equal(plainData[3].score, 14);
        assert.deepEqual(plainData[4].value, "item1");
        assert.equal(plainData[4].score, 1);
        assert.deepEqual(plainData[5].value, "comment2");
        assert.equal(plainData[5].score, 2);
        assert.deepEqual(plainData[6].value, "giraffe");
        assert.equal(plainData[6].score, 3);
        assert.deepEqual(plainData[7].value, [
          {
            content: "data:image/x-icon;base64,A=",
            name: "favicon.ico",
            type: "image/x-icon",
          },
        ]);
        assert.equal(plainData[7].score, 4);
        assert.deepEqual(plainData[8].value, { text1: "a", text2: "b" });
        assert.equal(plainData[8].score, 5);
        assert.deepEqual(plainData[9].value, {
          "Row 1": { "Column 1": 1, "Column 2": 2, "Column 3": 3 },
          "Row 2": { "Column 1": 4, "Column 2": 5, "Column 3": 4 },
        });
        assert.equal(plainData[9].score, 9);
    
        assert.deepEqual(plainData[10].value, {
          "Row 1": "Column 1",
          "Row 2": "Column 2",
        });
        assert.equal(plainData[10].score, 10);
        assert.equal(plainData[10].data[0].score, 1);
        assert.equal(plainData[10].data[1].score, 2);
    
        assert.deepEqual(plainData[11].value, [
          { question21: "Panel dynamic content 1" },
          { question21: "Panel dynamic content 2" },
        ]);
        assert.equal(plainData[11].score, 8);
    
        assert.ok(plainData[3].isNode);
        assert.equal(plainData[3].data.length, 2);
        assert.equal(plainData[3].data[0].value, "item2");
        assert.equal(plainData[3].data[0].displayValue, "item2");
        assert.equal(plainData[3].data[1].value, "item3");
        assert.equal(plainData[3].data[1].displayValue, "item3");
    
        Serializer.removeProperty("question", "score");
        Serializer.removeProperty("itemvalue", "score");
      }
    );
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多