本文介绍了在Qualtrics调查中,如何使用JavaScript动态设置滑块的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Qualtrics进行了一项调查,需要让我的项目根据变量显示滑块的不同值,在我的例子中,是循环和合并的值。这似乎不是你可以用管道文本做的事情,所以我必须弄清楚如何在Javascript中做到这一点。

I was making a survey in Qualtrics, and needed to have my items show different values of the slider depending on a variable, in my case, the value from a loop and merge. That didn't seem like a thing that you could do with piped text, so I had to figure out how to do it in Javascript.

我只是将此作为一个机会提供我自己找到的答案。与Qualtrics一样,您的里程可能会有所不同,这可能需要根据您的具体情况进行修改。特别是,问题ID和postTags会根据它是否处于循环/合并以及其他因素而改变。

I'm just posting this as an opportunity to provide the answer I found on my own. As usual with Qualtrics, your mileage may vary, and this may need to be modified for your specific situation. In particular, the question IDs and postTags change depending on whether it is in a loop/merge, and perhaps on other factors.

推荐答案

将以下代码放入问题的javascript部分:

Put the following code into the javascript section of the question:

// Set the slider range
// First define the function to do it
setSliderRange = function (theQuestionInfo, maxValue) {
    var postTag = theQuestionInfo.postTag
    var QID=theQuestionInfo.QuestionID
    // QID should be like "QID421"
    // but postTag might be something like "5_QID421" sometimes
    // or, it might not exist, so play around a bit.
    var sliderName='CS_' + postTag
    window[sliderName].maxValue=maxValue
    // now do the ticks. first get the number of ticks by counting the table that contains them
    var numTicks = document.getElementsByClassName('LabelDescriptionsContainer')[0].colSpan
        // do the ticks one at a time
        for (var i=1; i<=numTicks; i++) {
        var tickHeader='header~' + QID + '~G' + i
        // the first item of the table contains the minimum value, and also the first tick.
        // so we do some tricks to separate them out in that case.
        var tickSpanArray = $(tickHeader).down("span.TickContainer").children
        var tickSpanArrayLength=tickSpanArray.length
        var lastTickIndex=tickSpanArrayLength - 1
        var currentTickValue = tickSpanArray[lastTickIndex].innerHTML
        currentTickValue=currentTickValue.replace(/^\s+|\s+$/g,'')
        console.log('Tick value ' + i + ' is ' + currentTickValue)
        // now get the new value for the tick
        console.log('maxValue: ' + maxValue + ' numTicks: ' + numTicks + ' i: ' + i)
        var newTickValue = maxValue * i / numTicks //the total number of ticks
        tickSpanArray[lastTickIndex].innerHTML=newTickValue.toString()
        console.log('Changed tick value to ' + newTickValue)
    }
}

var currentQuestionInfo = this.getQuestionInfo()
var currentQuestionID = currentQuestionInfo.QuestionID
// Now call the function
setSliderRange(currentQuestionInfo, theMaxValueYouWant)

如果您觉得我的答案有用,请帮助提高我的声誉,以便添加 qualtrics作为有效标签!!或者,如果声誉超过1500的其他人愿意这样做会非常有帮助!

If you find my answers helpful, help raise my reputation enough to add "qualtrics" as a valid tag!! Or, if someone else with reputation over 1500 is willing to do it that would be very helpful!

这篇关于在Qualtrics调查中,如何使用JavaScript动态设置滑块的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:17