gpt4 book ai didi

javascript - 构建在选择列表中单击的项目数组并将值分配给元素

转载 作者:行者123 更新时间:2023-12-02 23:08:16 26 4
gpt4 key购买 nike

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

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")
.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);
$("#SubjectAreasOfProgramme").val(JSON.stringify(subjectAreas));
});
})
.trigger("change");

function logValue() {
console.log($("#SubjectAreasOfProgramme").val());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="hidden" asp-for="SubjectAreasOfProgramme" id="SubjectAreasOfProgramme" />
<select multiple style="width: 50px;">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<input id="subjectlist" />
<button onclick="logValue();">get SubjectAreasOfProgramme value</button>
<table id="overview">
</table>

关于javascript - 构建在选择列表中单击的项目数组并将值分配给元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488491/

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