gpt4 book ai didi

javascript - 在 Qualtrics Surveys 中,如何在单击下一个按钮时将对项目的响应保存到嵌入数据中?

转载 作者:搜寻专家 更新时间:2023-11-01 05:11:53 29 4
gpt4 key购买 nike

实际上,我只是花了很长时间从参差不齐的文档和大量 Web 控制台检查中拼凑出这个问题的答案。由于到目前为止最有用的信息来自 Stack Overflow,因此我想将这个问题及其答案放在这里,以供将来搜索的任何人受益。

问题是,如何将 javascript 添加到问题中,然后立即将该问题的答案保存到嵌入式数据字段中?立即,而不是等到下一个 Qualtrics.SurveyEngine.AddOnLoad()。这涉及到如何将 javascript 添加到调查问题并在单击“下一步”按钮时运行它的子问题?以及如何保存基于问题 ID 动态命名的嵌入式数据字段的子问题?

买者警告:Qualtrics 并未明确支持所有这些 javascript 黑客攻击,因此此处的代码最终可能会停止工作。

最佳答案

下面介绍了如何获取 slider 的响应值并将其保存在嵌入式数据字段中,该字段的名称根据问题 ID 进行编号。这种方式仅在 slider 的文本输入框可见时才有效。没有它也可能有一种方法,也许其他人可以将其添加到答案中。

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”最终保存所选选项的数字,而“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 代码。

要测试这些,请制作 slider 问题或多项选择题并添加相应的 javascript。然后,在分页符后制作另一张幻灯片(分页符是导致“下一步”按钮出现所必需的)。在该幻灯片中,放置一个管道文本字段,如 ${e://Field/result_1} 或警告对话框告诉您保存的值的任何内容。当您预览调查时,您应该会在第二个问题的管道文本字段中看到在第一个问题上输入的值。

就此演示而言,您当然可以仅使用管道文本实现相同的效果。但是,如果您出于某种原因想要一个问题真正立即将其响应保存到嵌入式数据中,这将为您做到这一点。

此外,如果您需要知道在单击“下一步”按钮时如何运行代码,这可以适应任何一般需要。

关于javascript - 在 Qualtrics Surveys 中,如何在单击下一个按钮时将对项目的响应保存到嵌入数据中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851340/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com