qualtrics JavaScript:使用先前的答案自动填充矩阵表 [英] qualtrics javascript: autopopulate matrix table using previous answers

查看:147
本文介绍了qualtrics JavaScript:使用先前的答案自动填充矩阵表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Qualtrics调查中问了相同的矩阵表(多次回答)问题两次.当我第二次向用户显示问题时,我希望从用户第一次回答问题起就自动填充答案.我已经查看了文档中的代码段,因此我知道如何选择当前问题中的复选框,例如

I ask the same matrix table (multiple answer) question twice in my Qualtrics survey. When I display the question to the user the second time, I want the answers to be auto populated from the first time the user answered the question. I've looked at the code snippets in the documentation, so I know how to select the checkboxes in the current question, e.g.

for (var i = 1; i <= rows; i++) {
    for (var j = 1; j <= cols; j++) {
        this.setChoiceValue(i, j, true);
    }
}

我的问题是我无法弄清楚如何从上一个问题中选择答案.我已经尝试过类似的方法,但是它不起作用(无法读取null的属性"attr"):

My issue is that I can't figure out how to get the selected answers from the previous question. I have tried something like this but it doesn't work (cannot read property 'attr' of null):

var isChecked = $("#QR~QID2~" + i + "~" + j").attr('checked');

文档中的此页建议使用管道文字,类似以下内容:

This page from the documentation suggests using piped text, something along the lines of:

var selectedChoice = "${q://QID2/ChoiceGroup/SelectedChoices}";

这似乎为我提供了已选择的调查行中的唯一陈述,但是我需要为问题中的每个陈述(行)获得选择的答案(col).我不确定如何制定正确的管道文字.

This seems to give me the unique statements in the rows of the survey that have been selected, but I need to get the selected answer (col) for each statement (row) in the question. I'm not sure how to formulate the correct piped text.

任何建议将不胜感激!谢谢.

Any advice would be much appreciated! Thanks.

这是我最终使用的代码.

Edit 1: Here is the code I ended up using.

Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place Your Javascript Below This Line*/
    var numChecks = $(this.getQuestionContainer()).select('input[type="checkbox"]');
    var numCols = 4;
    var numRows = numChecks.length / numCols;
    var map = {};

    //I won't have more than 20 rows in the matrix
    map[1] = "${q://QID53/SelectedAnswerRecode/1}";
    map[2] = "${q://QID53/SelectedAnswerRecode/2}";
    map[3] = "${q://QID53/SelectedAnswerRecode/3}";
    map[4] = "${q://QID53/SelectedAnswerRecode/4}";
    map[5] = "${q://QID53/SelectedAnswerRecode/5}";
    map[6] = "${q://QID53/SelectedAnswerRecode/6}";
    map[7] = "${q://QID53/SelectedAnswerRecode/7}";
    map[8] = "${q://QID53/SelectedAnswerRecode/8}";
    map[9] = "${q://QID53/SelectedAnswerRecode/9}";
    map[10] = "${q://QID53/SelectedAnswerRecode/10}";
    map[11] = "${q://QID53/SelectedAnswerRecode/11}";
    map[12] = "${q://QID53/SelectedAnswerRecode/12}";
    map[13] = "${q://QID53/SelectedAnswerRecode/13}";
    map[14] = "${q://QID53/SelectedAnswerRecode/14}";
    map[15] = "${q://QID53/SelectedAnswerRecode/15}";
    map[16] = "${q://QID53/SelectedAnswerRecode/16}";
    map[17] = "${q://QID53/SelectedAnswerRecode/17}";
    map[18] = "${q://QID53/SelectedAnswerRecode/18}";
    map[19] = "${q://QID53/SelectedAnswerRecode/19}";
    map[20] = "${q://QID53/SelectedAnswerRecode/20}";


    for (var i = 1; i <= numRows; i++) {
        //Get the recode values for row i
        var rowValues = map[i].split(",");
        //Loop through all the recode values for the current row
        for (var c = 0 ; c < rowValues.length; c++) {
            var val = parseInt(rowValues[c].trim());
            //Select the current question's checkboxes corresponding to the recode values
            this.setChoiceValue(i, val, true);
        }
    }
});

我现在有一些奇怪的行为.我正在尝试仅填充3行的表,所以我认为

I'm getting some strange behavior now. I'm trying to populate a table with only 3 rows, so I would think that

    "${q://QID53/SelectedAnswerRecode/1}";
    "${q://QID53/SelectedAnswerRecode/2}";
    "${q://QID53/SelectedAnswerRecode/3}";

将为我提供问题"QID53"的上一张表中前三行的值.但是实际上那些返回空字符串,并且直到调用

would give me the values for the first three rows from the previous table for question "QID53" . But actually those return empty strings, and it's not until calling

    "${q://QID53/SelectedAnswerRecode/5}";
    "${q://QID53/SelectedAnswerRecode/6}";
    "${q://QID53/SelectedAnswerRecode/7}";

我得到前三个值.

对于一个14行的表,在调用之前什么也不会返回

For a table of 14 rows, nothing returns until calling

"${q://QID53/SelectedAnswerRecode/4}";

并将表中的最后3行留空.

and it leaves the last 3 rows in the table empty.

我是否错误地认为"SelectedAnswerRecode"之后的数字是行号?我缺少补偿的东西吗?

Am I wrong in assuming that the number after "SelectedAnswerRecode" is the row number? Is there something about an offset that I'm missing?

推荐答案

我认为您想使用重新编码值.像这样:

I think you'll want to pipe in the recode values. Something like:

var r1ar = parseInt("${q://QID2/SelectedAnswerRecode/1}");
var r2ar = parseInt("${q://QID2/SelectedAnswerRecode/2}");

然后设置值:

this.setChoiceValue(1, r1ar, true);
this.setChoiceValue(2, r2ar, true);

确保您的重新编码值与列ID匹配.

Make sure your recode values match the column ids.

根据以下评论进行编辑/添加:

Edits/additions based on comments below:

  1. 在将页面发送到浏览器之前,javascript中的管道值已解析到服务器端,因此它们是固定值.没有办法使它们在javascript中变得动态.如果您的问题由于显示逻辑或结转而具有可变的行数,则必须在javascript中包括所有可能的管道值,然后检查它们以查看有效的值.

  1. Piped values in the javascript get resolved server side before the page is sent to the browser, so they are fixed values. There is no way to make them dynamic in javascript. If your question has a variable number of rows due to display logic or carryover, you'll have to include all the possible piped values in the javascript, then check them to see which ones are valid.

对于多重答案矩阵,您可以使用str.split(',')将逗号分隔的列表转换为数组,然后循环遍历该数组.Qualtric在逗号分隔的列表中的逗号后面包含空格,因此您必须trim()数组中的字符串.

For a multiple answer matrix, you can convert the comma separated list into an array using str.split(',') then loop through the array. Qualtrics includes spaces after the commas in comma separated lists, so you'll have to trim() the strings in the array.

这篇关于qualtrics JavaScript:使用先前的答案自动填充矩阵表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆