Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 构建在选择列表中单击的项目数组,并为元素赋值_Javascript_Jquery - Fatal编程技术网

Javascript 构建在选择列表中单击的项目数组,并为元素赋值

Javascript 构建在选择列表中单击的项目数组,并为元素赋值,javascript,jquery,Javascript,Jquery,我已经具备了构建数组的功能,但我不知道如何将数组中的值分配给另一个列表 var subjectAreas = []; $("#subjectlist") .change(function () { $("select option:selected").each(function () { $('#overview').append("<tr id='" + this.value + "'><td>" +

我已经具备了构建数组的功能,但我不知道如何将数组中的值分配给另一个列表

var subjectAreas = [];

$("#subjectlist")
     .change(function () {
            $("select option:selected").each(function () {
                $('#overview').append("<tr id='" + this.value + "'><td>" + 
                $(this).text() + "</td><td id=" + this.value + ">" "</td> 
                </tr>");
                subjectAreas.push(this.value);    

                //below is the part I don't understand
                $("#SubjectAreasOfProgramme")
                .val(subjectAreas.values.arguments);
            });
     })
.trigger("change");

// below has the id: SubjectAreasOfProgramme and the behind variable is of type List
<input type="hidden" asp-for="SubjectAreasOfProgramme" />
您可以使用JSON.stringify将数组转换为字符串,并将最新的值分配给隐藏的输入,您可能忘记了为隐藏的输入提供id。通过选择选项,输入文本并单击按钮,可以测试以下代码段:

var subjectAreas=[]; $subjectlist .changefunction{ $select选项:selected.eachfunction{ $“概述”。追加+ $this.text++; subjectAreas.pushthis.value; $subjectareasofprogram.valJSON.stringifysubjectAreas; }; } .触发变化; 函数对数值{ console.log$subjectareasofprogram.val; } 1. 2. 3. 4. 5. 6. 7. 获取程序值的主题区域
ID必须是唯一的。如果我控制台记录$subjectareasofprogram.val,它只包含单击的最后一项。请您进一步澄清您的问题,以便我能更好地了解?您希望所有选定值从选择列表到隐藏字段。我说得对吗@Ryn9011@Ryn9011很高兴这有帮助: