在Qualtrics调查中,如何使用JavaScript动态设置滑块的范围?

时间:2014-04-27 01:35:43

标签: javascript qualtrics

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

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

1 个答案:

答案 0 :(得分:3)

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

// 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)

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