【问题标题】:Set embedded data in Qualtrics with javascript for use with display logic (need updates?)使用 javascript 在 Qualtrics 中设置嵌入数据以用于显示逻辑(需要更新?)
【发布时间】:2018-02-15 23:53:15
【问题描述】:

虽然此问题已多次解决 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) 对替代按钮解决方案的更新,该解决方案将从同一页面上的问题中获取计数。

【问题讨论】:

  • 我建议通过将模糊事件侦听器添加到输入字段来即时计算和更新计数。您无需对“下一步”按钮或隐藏页面进行任何操作。除了初始化计数之外,这两个问题的脚本基本上是相同的。
  • @T.Gibbons,是的,这行得通。我在答案中包含了完整的代码。谢谢!

标签: javascript jquery qualtrics


【解决方案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,一切顺利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多