微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Qualtrics中的Javascript:无法保存JS生成的嵌入式字段?

如何解决Qualtrics中的Javascript:无法保存JS生成的嵌入式字段?

我已经在Qualtrics中实现了一个实验,在该实验中,我使用Javascript从数组中为多个“属性”绘制随机值,并随机化了属性的顺序。简而言之,我采用随机绘制的值,将这些值放入它们自己的数组中,然后将值洗牌到新数组中,然后将它们另存为字符串在嵌入式数据字段中。一种复杂性是,不向参与者展示属性中的两个属性(种族和性别)的随机抽取值,而是将其用于为属性名称)创建一组可能的值,这些属性值被呈现给参与者。

我的代码对大多数问卷调查者来说都按预期工作,但是偶尔我导出的数据显示有人到达了运行Javascript却未嵌入数据字段的页面(例如,在我的示例中为“ attr_viewed_names_string”和“ attr_all_id_string”)空的。我已经在移动设备和台式机上完成了很多次调查,但是无法复制该过程,从而导致这些嵌入式字段的值无法显示在我的数据中。最大的问题是我不知道(a)代码实际上没有生成字符串,还是(b)字符串没有保存到嵌入式字段。

下面是我在调查的第一页上实现的简化版本,以创建字符串并保存它们。我是否错过了一些代码无法保存属性名称字符串(例如“名称|宗教|种族|性别”)的场景

Qualtrics.SurveyEngine.addOnload(function()
{

/////////////////////////////////////////////////////
// DEFINE HELPERS
/////////////////////////////////////////////////////

// Function to randomize the ordering of the elements in arrays
   function shuffle(array){
        var counter = array.length,temp,index;
        while (counter > 0){
            index = Math.floor(Math.random() * counter);
            counter = counter-1;
            temp = array[counter];
            array[counter] = array[index];
            array[index] = temp;
        }
        return array;
    };
    
    
// Function to sample K elements from an array without replacement
// This is used for grabbing values for our attributes.
 function getRandom(arr,n) {
    var result = new Array(n),len = arr.length,taken = new Array(len);
    if (n > len)
        throw new RangeError("getRandom: more elements taken than available");
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
};
    
// Function that,given an array X containing arrays 1,2,3...k,// randomly select one element from each 1,3,...k
// and returns an array containing the values. Checks that
// it is an array and returns the original object if the element is
// not an array.
    function get1RandomForEach(array){
        return array.map(attr => Array.isArray(attr) ? getRandom(attr,1) : attr);
    }
 
/////////////////////////////////////////////////////
// SPECIFY STUDY ParaMETERS
/////////////////////////////////////////////////////
   
// Number of choice tasks each participant completes
var choice_task_n = 1

// Number of profiles in total each participant will observe
var profiles_total = choice_task_n*2

/////////////////////////////////////////////////////
// CREATE ATTRIBUTES
/////////////////////////////////////////////////////
   
// Create the attributes and set of possible values for each
// attribute.
var religion = ["val1","val2"];
var race = ["B","W"];
var gender = ["F","M"];    

// Create array of objects where each object contains key-value pairs
// for a name,race,and gender
var name_table = [{full_name: "name1",race: "W",gender: "F"},{full_name: "name2",race: "B",{full_name: "name3",gender: "M"},{full_name: "nam4",gender: "M"}];
    
// Create an array of objects containing all attributes participants will
// observe,an attribute identifier,and the possible values the variables
// can assume. Note that we assign the value "placeholder" to Name since
// the values depend on the gender and race.

var attr_table = [
    {attrib: "ReligIoUs Identification",category: "relig",vals: religion},{attrib: "Name",category: "name",vals: "placeholder"}
    ];
    
// Shuffle the order of the attributes
var attr_table_shuffled = shuffle(attr_table)

// Create arrays with the attribute names,attribute text,and 
// attribute values. This will be for the variables participants
// observe (in contrast with unobserved set that includes gender
// and race but excludes name)
var attr_viewed_vals = attr_table_shuffled.map(function(e){return e.vals;});
var attr_viewed_names = attr_table_shuffled.map(function(e){return e.attrib;});
var attr_viewed_id = attr_table_shuffled.map(function(e){return e.category;});

// Convert to delimited string for saving
var attr_viewed_names_string  = attr_viewed_names.join("|");
var attr_viewed_id_string  = attr_viewed_id.join("|");

// Add unobserved attributes' IDs to the id string to
// get all attributes' ids
var attr_all_id_string = attr_viewed_id_string + "|race|gender";

// Store the string in embedded data
Qualtrics.SurveyEngine.setEmbeddedData("attr_viewed_names_string",attr_viewed_names_string);
Qualtrics.SurveyEngine.setEmbeddedData("attr_viewed_id_string",attr_viewed_id_string);
Qualtrics.SurveyEngine.setEmbeddedData("attr_all_id_string",attr_all_id_string);
    
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。