使用javascript在Qualtrics中设置嵌入数据以用于显示逻辑(需要更新?)

时间:2018-02-15 23:53:15

标签: javascript jquery qualtrics

虽然此问题已经解决了herehere几次,但随后的Qualtrics更新仍然难以继续应用这些解决方案。

我的目标:计算包含2个问题的答案的文本框的数量,并使用该数字来确定在第三个问题中显示的内容。分隔第二个和第三个问题的页面上的文本问题类型中使用的以下javascript代码可以正常工作:

Qualtrics.SurveyEngine.addOnload(function()
{
  //Count up the number of text boxes with anything in them
  var count = 0;
  if('${q://QID1/ChoiceTextEntryValue/1}') count++;
  if('${q://QID1/ChoiceTextEntryValue/2}') count++;
  //etc...
  if('${q://QID2/ChoiceTextEntryValue/1}') count++;
  if('${q://QID2/ChoiceTextEntryValue/2}') count++;
  //etc...
  //Set count as embedded data (added to survey flow earlier)
  Qualtrics.SurveyEngine.setEmbeddedData('count', count);   
};

问题是我在第二和第三个问题之间有一个无用的页面。如果我使用显示逻辑用这段代码隐藏我的干预问题,代码就不会执行。如果我使用javascript来隐藏问题并按照here所述自动移至下一页,则可以使用,但是之后用户无法返回调查,因为jQuery('#NextButton').click();会将其返回到第三个问题。我已尝试将上述代码包含在替代NextButton代码中,并将其移至与QID2相同的页面,如here所示,至少有一个我可以理解的更新:

this.hideNextButton ();
$('NextButton').insert({
before: "<input id=\"checkButton\" type=\"button\" value=\"  ->  \" title=\"  ->  \">"
}); 
$('checkButton').onclick = function() {
//Previous count and set code here
$('NextButton').click();
};

这适用于从QID1(位于上一页)计算所有内容,但不会从QID2(同一页面)中捕获这些内容。

我也在addOnReadyaddOnUnload中放置代码时无动于衷。

我需要的是1)对解决方案的更新,该解决方案隐藏了一个介入页面上的问题,该页面修改了页面上的“后退”按钮,我的第三个问题是将参与者两个页面踢回来,或者2)替换按钮解决方案的更新,该解决方案将从同一页面上的问题中获取计数。

1 个答案:

答案 0 :(得分:1)

根据@ T.Gibbons给出的提示,我能够让事情顺利进行。

在调查流程中包含count1作为嵌入数据之后的第一个问题的代码:

//Count up the number of text boxes with anything in them
var qid = this.questionId; //Pull question id
//Focus on a text box to force listener to fire; ensures downstream logic will work regardless of participant behavior
document.getElementById('QR~'+qid+'~1').focus();
var quest = document.getElementById(qid); //Set up listener
quest.addEventListener('blur', function() {
    var count1 = 0;
    if(document.getElementById('QR~'+qid+'~1').value) count1++; 
    if(document.getElementById('QR~'+qid+'~2').value) count1++; 
    //and so on...
    //Set count1 as embedded data
    Qualtrics.SurveyEngine.setEmbeddedData('count1', count1);
}, {capture: true}); 

在调查流程中包含count之后的第二个问题的代码:

//Count up the number of text boxes with anything in them, add to prior question
var qid = this.questionId; //Pull question id
var count1 = Qualtrics.SurveyEngine.getEmbeddedData('count1'); //Pull count1 from previous question
document.getElementById('QR~'+qid+'~1').focus(); //Focus on a text box, force listener to fire
//Set up listener
var quest = document.getElementById(qid);
quest.addEventListener('blur', function() {
    var count2 = 0;
    if(document.getElementById('QR~'+qid+'~1').value) count2++; 
    if(document.getElementById('QR~'+qid+'~2').value) count2++; 
    // and so on... 
    var count = count1 + count2;
    //Set count as embedded data
    Qualtrics.SurveyEngine.setEmbeddedData('count', count);
}, {capture: true}); 

将调查逻辑设置为依赖count,这样做很好。

相关问题