问题
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.
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);
};
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();
};
This works for counting up everything from QID1 (located on the previous page), but doesn't catch those from QID2 (same page).
I've also tinkered with placing code in addOnReady
and addOnUnload
to no avail.
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.
回答1:
With the hint given by @T.Gibbons, I was able to get things working.
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});
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});
Set the survey logic to depend on count
and it's good to go.
来源:https://stackoverflow.com/questions/48818156/set-embedded-data-in-qualtrics-with-javascript-for-use-with-display-logic-need