Qualtrics:自定义 JavaScript 在将问题导入新调查时停止工作

Qualtrics: Custom JavaScript ceases to work when importing questions to a new survey

我有一个 Qualtrics survey,其中包含一些自定义问题 JavaScript(以启用带有两个滑块旋钮的滑块)。我可以 a) 复制调查以及 b) 将调查导出为 .qsf 文件并重新导入。在这两种情况下,我都会得到一个新的有效调查。

但是,使用 "Import Questions From..." 功能将调查问题导入现有调查不起作用; JavaScript 在这种情况下不起作用。有没有办法将这些问题导入现有调查,同时保留 JavaScript?

第一个(最相关)问题中使用的代码:

Qualtrics.SurveyEngine.addOnload(function()
{

    document.getElementById("QR~QID7").setAttribute("readonly", "readonly");
    document.getElementById("QR~QID8").setAttribute("readonly", "readonly");
    var surface;
    var cnst = 4;
    // Sets the sliders parameters and starting values
     $("#research-slider").slider({ id: "research-slider", min: 0, max: 10, range: true, value: [0, 10]});
     // variable to store in surface area when user has stopped sliding
     var surface, currentResponse;
     $("#research-slider").on("slideStop", function(slideEvt) {
         surface = slideEvt.value;
         document.getElementById("minimum").innerHTML  = surface[0];
         document.getElementById("maximum").innerHTML  = surface[1];
         document.getElementById("newValue").innerHTML  = (surface[1]- surface[0])/cnst ;
         document.getElementById("QR~QID7").value  = surface[0];
         document.getElementById("QR~QID8").value  = surface[1];
     });



     $('NextButton').onclick = function (event) {       
        // and now run the event that the normal next button is supposed to do
        Qualtrics.SurveyEngine.navClick(event, 'NextButton')
    }

})

您的 JavaScript 在导入时将无法使用,因为您使用的是固定 QID 代码(QID7 和 QID8)。解决方案是编写代码,通过遍历 DOM 找到正确的元素。最简单的方法是使用 prototypejs 而不是 native JavaScript.

假设最小值 (QID7) 和最大值 (QID8) 紧跟在您的代码所附的问题之后,那么它将类似于:

var qid = this.questionId;
var min = $(qid).next('.QuestionOuter').down('.InputText');
var max = $(qid).next('.QuestionOuter',1).down('.InputText');
min.setAttribute("readonly", "readonly");
max.setAttribute("readonly", "readonly");
var surface;
var cnst = 4;
// Sets the sliders parameters and starting values
 $("#research-slider").slider({ id: "research-slider", min: 0, max: 10, range: true, value: [0, 10]});
 // variable to store in surface area when user has stopped sliding
 var surface, currentResponse;
 $("#research-slider").on("slideStop", function(slideEvt) {
     surface = slideEvt.value;
     $("minimum").innerHTML  = surface[0];
     $("maximum").innerHTML  = surface[1];
     $("newValue").innerHTML  = (surface[1]- surface[0])/cnst ;
     min.value  = surface[0];
     max.value  = surface[1];
 });