Survey JS射电计算

Survey JS Radio Calculation

使用 Survey JS,其想法是要求用户从预定义的选项列表(单选按钮)中回答一些问题。

每个选项都会分配一个分数(0、25、75、100),因为每次选择时我都想在屏幕上的某个地方显示实时比分。

我知道我需要创建一个类似于 this example 的表达式,但我不确定从哪里开始。

我已经创建了表单的基本结构,可以seen here

$.material.init();

var json = {
  title: "Audit Example",
  showProgressBar: "top",
  pages: [{
    questions: [{
      type: "matrix",
      name: "Quality",
      title: "Please score each department below.",
      columns: [{
        value: 0,
        text: "None"
      }, {
        value: 25,
        text: "Isolated"
      }, {
        value: 50,
        text: "Some"
      }, {
        value: 100,
        text: "Widespread"
      }],
      rows: [{
        value: "training",
        text: "Training"
      }, {
        value: "support",
        text: "Support"
      }, {
        value: "safety",
        text: "Safety"
      }, {
        value: "communication",
        text: "Communication"
      }]
    }]
  }
  ]
};

Survey.defaultBootstrapMaterialCss.navigationButton = "btn btn-green";
Survey.defaultBootstrapMaterialCss.rating.item = "btn btn-default my-rating";
Survey.Survey.cssType = "bootstrapmaterial";

var survey = new Survey.Model(json);

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

survey.render("surveyElement");

如有任何建议,我们将不胜感激。

假设您的问题是动态显示元素总和。

为此,您可以像这样注册自己的函数:

function sumInObject(params) {
  if (params.length != 1) return 0;
  if(!params[0]) return 0;
  const object = params[0];
  const array = Object.keys(object)
    .map(key => object[key])
    .map(value => parseInt(value));
  return array.reduce((left, right) => left + right);
}

Survey.FunctionFactory.Instance.register("sumInObject", sumInObject);

然后像这样使用它:

}, {
    "type": "expression",
    "name": "total",
    "title": "Total Quality:",
    "expression": "sumInObject({Quality})",
    "displayStyle": "decimal",
    "startWithNewLine": true
}

Full code in Plunk

希望对您有所帮助。