我在 header 部分定义了一个自定义函数,用于检查、提醒用户,并在特定 slider 未满足某些限制时重置其值。
当问题点击时调用此函数时效果非常好:
this.questionclick = chkVals;
我还想在用户退出文本输入字段时运行该函数(因为有些用户正在使用键盘进行调查)。我为每个 slider 的文本输入字段实现了一个事件监听器,当焦点离开文本输入字段时,该监听器运行该函数。
// choices is an array of choice ids
for (i = 0; i < choices.length; i++) {
var x = document.getElementById(choices[i]);
x.addEventListener("blur", chkVals, true);
};
我知道事件监听器可以工作,因为会弹出正确的警报。它只是无法重置值,因为 this.setChoiceValue
不是环境中的函数。我尝试在函数中设置 var that = this;
并调用 that.setChoiceValue
,但仍然不起作用。
任何帮助将不胜感激!
最佳答案
您尚未显示所有代码,因此我做了一些假设。
this
是 addOnload 函数中的 Qualtrics 问题对象。由于 chkVals 位于 addOnload 函数之外,因此 this
(或 that
)未定义。因此,您需要在函数调用 (function chkVals(qobj)
) 中传递它,然后在 chkVals 函数中使用 qobj.setChoiceValue
。然后你的函数调用变成:
this.questionClick = chkVals(this);
和
x.addEventListener("blur", chkVals(this), true);
关于javascript - Qualtrics API 函数在 EventListener 中调用的自定义函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396245/