我实际上花了很长时间才把这个问题的答案从错误的文档和大量的Web控制台检查中拼凑出来。由于到目前为止最有用的信息来自Stack Overflow,我想把这个问题及其答案放在这里,以便其他任何人都能从中获益。
问题是,如何将javascript添加到一个问题中,该问题会立即将该问题的答案保存到嵌入数据字段中?立即,不要等到下一个Qualtrics.SurveyEngine.AddOnLoad()。这涉及到如何在调查问题中添加javascript并在单击“下一步”按钮时运行它的子问题?还有一个子问题,如何保存基于问题ID动态命名的嵌入数据字段?
Caveat Emptor:Qualtrics没有明确支持所有这些javascript黑客攻击,因此这里的代码最终可能会停止工作。
答案 0 :(得分:8)
以下是如何获取滑块的响应值并将其保存在嵌入数据字段中,其名称根据问题ID进行编号。这种方式仅在滑块的文本输入框可见时才有效。如果没有它,可能还有一种方法可以做到,也许其他人可以将其添加到答案中。
Qualtrics.SurveyEngine.addOnload(function()
{
// everything here runs when the page is loaded. So, we can get the Question ID here,
// but we can't get the response value until later.
var currentQuestionID = this.getQuestionInfo().QuestionID
//var resultEmbeddedName = currentQuestionID + "_result" //e.g. QID6_result
var resultEmbeddedName = "result_" + currentQuestionID.substring(3) //e.g. result_6
$('NextButton').onclick = function (event) {
// everything in here will run when you click the next button
// note that it has access to javascript variables from the enclosing function
// however, if you declare something in here with "var" then it will be a LOCAL variable
// the following alert will appear when you click the next button. For me, it appears twice;
// I'm not sure why.
// Save the current question's response value
var responseTextField = document.getElementById(currentQuestionID + '~1~result')
var currentResponse = responseTextField.value
alert("Result: " + currentResponse + "\nwill be available to future questions as: \n$" + "{e://Field/" + resultEmbeddedName + "}")
Qualtrics.SurveyEngine.setEmbeddedData(resultEmbeddedName, currentResponse)
// and now run the event that the normal next button is supposed to do
Qualtrics.SurveyEngine.navClick(event, 'NextButton')
}
});
对于多项选择,它有点复杂。这是有效的代码,有很多评论和演示。注意变量" currentResponse"最终拿着所选择的NUMBER,而" currentChoiceText"最后拿着该选项的文字标签(例如是或否)。所以你可以保存你喜欢的任何一个。
Qualtrics.SurveyEngine.addOnload(function()
{
// everything here runs when the page is loaded. So, we can get the Question ID here,
// but we can't get the response value until later.
var currentQuestionID = this.getQuestionInfo().QuestionID
console.log("Current Question ID is: " + currentQuestionID)
//var resultEmbeddedName = currentQuestionID + "_result" //e.g. QID6_result
var resultEmbeddedName = "result_" + currentQuestionID.substring(3) //e.g. result_6
$('NextButton').onclick = function (event) {
// everything in here will run when you click the next button
// note that it has access to javascript variables from the enclosing function
// however, if you declare something in here with "var" then it will be a LOCAL variable
// the following alerts will appear when you click the next button. For me, it appears twice;
// I'm not sure why.
// Save the current question's response value
var questionObject = Qualtrics.SurveyEngine.getInstance(currentQuestionID)
var currentResponse = questionObject.getSelectedChoices()[0] //in case more than one is selected, it will only work here to take one!
var theQuestionInfo=questionObject.getQuestionInfo()
var choicesObject=theQuestionInfo.Choices
var thisChoiceObject=choicesObject[currentResponse]
var currentChoiceText=thisChoiceObject.Text
console.log("Number of the current choice is " + currentResponse)
console.log("Text of the current choice is " + currentChoiceText)
alert("Result: " + currentChoiceText + "\nwill be available to future questions as: \n$" + "{e://Field/" + resultEmbeddedName + "}")
Qualtrics.SurveyEngine.setEmbeddedData(resultEmbeddedName, currentChoiceText)
// and now run the event that the normal next button is supposed to do
Qualtrics.SurveyEngine.navClick(event, 'NextButton')
}
});
这两个都动态引用了问题ID,因此如果您将问题复制到新问题,它们将单独工作。无需更改任何javascript代码。
要测试这些,请制作滑块问题或多项选择题并添加相应的javascript。然后,在分页符后再制作一张幻灯片(需要分页才能显示“下一步”按钮的外观)。在该幻灯片中,放置一个管道文本字段,如$ {e:// Field / result_1}或警告对话框告诉您保存的值。预览调查时,您应该会在第二个问题的管道文本字段中看到第一个问题上输入的值。
就这个演示而言,你可以通过管道文本实现同样的效果。但是如果你有一些理由想要问题实际上立即将其响应保存到嵌入数据中,那么这将为你做到这一点。
此外,如果您在单击“下一步”按钮时需要知道如何运行代码,则可以根据需要对其进行调整。
答案 1 :(得分:0)
我遇到问题,在$('NextButton')中调用下面的代码.onclick方法强制$('NextButton')。onclick方法被调用两次。删除它对我有用,因此onclick方法只被调用一次,但仍然会向前移动调查。
// and now run the event that the normal next button is supposed to do
Qualtrics.SurveyEngine.navClick(event, 'NextButton')
答案 2 :(得分:0)
每次按“下一步”按钮时都有效。希望有所帮助!
Qualtrics.SurveyEngine.addOnload(function(){
var nextButton =document.getElementById("NextButton");
if (nextButton!=null){
$("NextButton").on("click", function(event) {
alert("Hello World!");
});
}
});