简体   繁体   English

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

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

I have a Qualtrics survey containing a few questions with custom JavaScript (to enable a slider with two slider knobs). 我有一个Qualtrics调查,其中包含一些有关自定义JavaScript的问题(以启用带有两个滑块的滑块)。 I can a) copy the survey as well as b) export the survey as a .qsf file and re-import it. 我可以a)复制调查,以及b)将调查导出为.qsf文件并重新导入。 In both cases, I get a new, working survey. 在这两种情况下,我都会得到一份新的工作调查。

However, importing the survey questions to an existing survey using the "Import Questions From..." function does not work; 但是,无法使用“从...导入问题”功能将调查问题导入到现有调查中。 the JavaScript fails to work in this case. JavaScript无法在这种情况下运行。 Is there a way to import these questions to an existing survey while preserving the JavaScript? 是否可以在保留JavaScript的情况下将这些问题导入到现有调查中?

The code used in the first (most relevant) question: 第一个(最相关)问题中使用的代码:

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')
    }

})

Your JavaScript won't work when imported because you are using fixed QID codes (QID7 and QID8). 导入时,JavaScript无法使用,因为您使用的是固定的QID代码(QID7和QID8)。 The solution is to write your code to find the correct elements by walking the DOM. 解决方案是编写代码,以遍历DOM来查找正确的元素。 The easiest way is to use prototypejs instead of native JavaScript. 最简单的方法是使用prototypejs而不是本机JavaScript。

Assuming the min (QID7) and max (QID8) follow immediately after the question your code is attached to, then it would be something like: 假设在您的代码附加到问题后立即遵循最小值(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];
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM