2014-04-27 44 views
2

我在Qualtrics中進行了一項調查,並且需要讓我的項目根據變量顯示不同的滑塊值,在我的情況下,是循環和合並的值。這看起來不像是你可以用管道文本做的事情,所以我必須弄清楚如何在Javascript中做到這一點。在Qualtrics調查中,如何使用JavaScript動態設置滑塊的範圍?

我只是發佈這個機會來提供我自己找到的答案。像往常一樣,您的里程可能會有所不同,這可能需要根據您的具體情況進行修改。具體來說,問題ID和postTag會根據是否處於循環/合併以及其他因素而發生變化。

回答

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) 

如果你覺得我的回答可以幫助的,有助於提高我的名聲足以增加「qualtrics」作爲一個有效的標籤!或者,如果其他聲譽超過1500的人願意這樣做,那將非常有幫助!