使用JavaScript在Qualtrics中设置嵌入数据,以与显示逻辑配合使用(需要更新吗?) [英] Set embedded data in Qualtrics with javascript for use with display logic (need updates?)

查看:338
本文介绍了使用JavaScript在Qualtrics中设置嵌入数据,以与显示逻辑配合使用(需要更新吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管已多次解决此问题,但此处,随后的Qualtrics更新使继续应用这些解决方案变得困难.

While this issue has been addressed a few times here and here, subsequent Qualtrics updates have made it difficult to continue to apply these solutions.

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

My objective: count up the number of text boxes containing responses from 2 questions and use that number to determine what gets displayed in a third. The following javascript code used in a text question type on a page separating the second and third questions works fine:

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隐藏问题并按照此处所述自动转到下一页,可以,但是用户不能在调查中后退,因为jQuery('#NextButton').click();将他们返回到第三个问题.我尝试将上面的代码包装在替换的NextButton代码中,并将其移至带有QID2的同一页面,如

The problem is that I have a useless page between my second and third questions. If I use display logic to hide my intervening question with this code, the code doesn't execute. If I use javascript to hide the question and automatically move to the next page as described here, it works, but then the user can't move back in the survey because jQuery('#NextButton').click(); returns them to the third question. I've tried wrapping the above code in a substitute NextButton code and moving it to the same page with QID2 as shown here with at least one update that I could figure out:

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(同一页面)中的所有内容.

This works for counting up everything from QID1 (located on the previous page), but doesn't catch those from QID2 (same page).

我也曾尝试将代码放置在addOnReadyaddOnUnload中无济于事.

I've also tinkered with placing code in addOnReady and addOnUnload to no avail.

我需要的是1)更新解决方案以将问题隐藏在中间页面上,该页面用我的第三个问题修改页面上的后退"按钮,以使参与者两个页面退回,或2)对替代按钮解决方案的更新,该解决方案将从同一页面上的问题中获取计数.

What I need is either 1) an update to the solution that hides the question on an intervening page that modifies the Back button on the page with my third question to kick the participant two pages back, or 2) an update to the substitute button solution that will grab counts from the question on the same page.

推荐答案

有了@ T.Gibbons的提示,我能够使事情正常进行.

With the hint given by @T.Gibbons, I was able to get things working.

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

Code for the first question, after including count1 as embedded data in the Survey Flow:

//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之后:

Code for the second question, after including count in the Survey Flow:

//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,这很好.

Set the survey logic to depend on count and it's good to go.

这篇关于使用JavaScript在Qualtrics中设置嵌入数据,以与显示逻辑配合使用(需要更新吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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