Integrating keyboard use with answer randomization

2019-09-01 08:46发布

问题:

I'm using qualtrics to present a user with two choices, one on the left and one on the right. I'd like to have them use the keyboard arrow keys to make a selection and I currently have code to let them do this using the following:

Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place Your Javascript Below This Line*/

this.hideNextButton();
this.hidePreviousButton();

var that = this;

Event.observe(document, 'keydown', function keydownCallback(e) {
    var qid = this.questionId;
    var order = $("QR~"+qid+"~DisplayOrder").value.split("|");
    var left = order[0];
    var right = order[1];


  switch (e.keyCode) {
    case 37: // 'L' was pressed
      choiceID = left
      break;
    case 39: // 'R' was pressed
      choiceID = right
      break;
  }

  if (choiceID) {
    Event.stopObserving(document, 'keydown', keydownCallback);
    that.setChoiceValue(choiceID, true);
    that.clickNextButton();
  }
});

    });

If the answers are randomized and questions are not reversed it works fine. If the answers are reversed it will select the option on the right when the left arrow key is pressed, and vice versa. I suspect I need some way to set the choice ID to the ID of what is actually presented, or completely change the third and sixth lines, but some of the more promising functions in the API don't seem to pan out. Suggestions?

回答1:

You can get the display order of the randomized choices, then assign values to left and right (I'm assuming this is a simple multiple choice question):

var qid = this.questionId;
var order = $("QR~"+qid+"~DisplayOrder").value.split("|");
var left = order[0];
var right = order[1];

Then, in your code:

switch (e.keyCode) {
    case 37: // 'L arrow' was pressed
        choiceID = left
        break;
    case 39: // 'R arrow' was pressed
        choiceID = right
        break;
}