Javascript 需要创建一个函数,用于根据包含给定数字的数组分配集合选择值

Javascript 需要创建一个函数,用于根据包含给定数字的数组分配集合选择值,javascript,qualtrics,Javascript,Qualtrics,我对JavaScript完全陌生。。。。所以请容忍我 我正在尝试使用JavaScript预先填充参与者在调查早期所做的多项选择题的答案。请注意,我的变量“Legchoice”将采用管道输送到此处的值。但是,可以有多个值,因为受访者可以为要输入的问题选择多个答案。我知道我的剧本在问“LegChoice”是否等于给定的数字。。。。如何更改它,使“LegChoice”包含给定的数字。如果受访者选择与值1和3相对应的答案,那么我可以设置选择值4,5,6,7,8,9。或者,如果他们选择了与2和4对应的答案

我对JavaScript完全陌生。。。。所以请容忍我

我正在尝试使用JavaScript预先填充参与者在调查早期所做的多项选择题的答案。请注意,我的变量“Legchoice”将采用管道输送到此处的值。但是,可以有多个值,因为受访者可以为要输入的问题选择多个答案。我知道我的剧本在问“LegChoice”是否等于给定的数字。。。。如何更改它,使“LegChoice”包含给定的数字。如果受访者选择与值1和3相对应的答案,那么我可以设置选择值4,5,6,7,8,9。或者,如果他们选择了与2和4对应的答案呢?我希望这是有道理的

谢谢大家!

var LegChoice = "${q://QID23/SelectedChoicesRecode}";
var LegChoice = parseInt(LegChoice);


    if (LegChoice==1) {
    this.setChoiceValue(4,true);
    this.setChoiceValue(5,true);
    this.setChoiceValue(6,true);
    this.setChoiceValue(7,true);
}

if (LegChoice== 2) {
    this.setChoiceValue(4,true);
    this.setChoiceValue(5,true);
    this.setChoiceValue(6,true);
    this.setChoiceValue(7,true);
}

if (LegChoice==3) {
    this.setChoiceValue(6,true);
    this.setChoiceValue(8,true);
    this.setChoiceValue(9,true);
}

if (LegChoice == 4) {
    this.setChoiceValue(1,true);
    this.setChoiceValue(3,true);
}

您需要将LegChoice拆分为单独的答案,然后循环使用它们

var LegChoices = "${q://QID23/SelectedChoicesRecode}";
var that = this;
LegChoices.split(",").each(function(LegChoice,index) {
    var LegChoice = parseInt(LegChoice.trim());
    if (LegChoice==1) {
        that.setChoiceValueByRecodeValue(4,true);
        that.setChoiceValueByRecodeValue(5,true);
        that.setChoiceValueByRecodeValue(6,true);
        that.setChoiceValueByRecodeValue(7,true);
    }

    //etc...

});

注意:如果您是管道中的重编码值,则需要通过重编码值进行设置。

我认为更好的框架方式是。。。如果传入的值是一个数字数组。。。。如何将我的变量设置为如果Legchoice包含1,则此.setchoice值。。。。。如果Legchoice包含1,2,3,4,那么…值得记住的是Qualtrics将以逗号分隔的字符串返回多个答案。此外,Java和JavaScript非常不同。Qualtrics不使用Java。