Javascript 在多选下拉列表中获取所选值并将其插入JSON

Javascript 在多选下拉列表中获取所选值并将其插入JSON,javascript,html,json,Javascript,Html,Json,我输入了这样一本书的数据 身份证件 利弗尔税 孔特努 大奖赛 联合国体裁的选择 RomanBiographieComédienecycliquemédicaleScienceCulturePolitique因此,您成功地从输入元素中获取了值,因此我们将首先修改您的代码以检索它们: const addGenreTextbox = document.getElementById('selectGenre'); let result = {}; for (var option of addGen

我输入了这样一本书的数据


身份证件
利弗尔税
孔特努
大奖赛
联合国体裁的选择

RomanBiographieComédienecycliquemédicaleScienceCulturePolitique
因此,您成功地从输入元素中获取了值,因此我们将首先修改您的代码以检索它们:

const addGenreTextbox = document.getElementById('selectGenre');

let result = {};
for (var option of addGenreTextbox.options) {
    if (option.selected) {
        result[id] = addGenreTextbox.value.trim();
        result[Nom] = option[addGenreTextBox.selectedIndex].text.trim();
    }
}

item.Genre.push(result);

那应该对你有用。如果您有任何问题或我遗漏了什么,请告诉我。

因此,您成功地从输入元素中获取了值,因此我们将首先修改代码以检索它们:

const addGenreTextbox = document.getElementById('selectGenre');

let result = {};
for (var option of addGenreTextbox.options) {
    if (option.selected) {
        result[id] = addGenreTextbox.value.trim();
        result[Nom] = option[addGenreTextBox.selectedIndex].text.trim();
    }
}

item.Genre.push(result);

那应该对你有用。如果您有任何问题或我遗漏了什么,请告诉我。

这是正确的解决方案:

const addGenreTextbox = document.getElementById('selectGenre');

let GenreChoix = []
for (var i = 0; i < addGenreTextbox.options.length; i++) {
    if (addGenreTextbox.options[i].selected) {
        let result = {};
        result["id"] = addGenreTextbox.options[i].value;
        result["Nom"] = addGenreTextbox.options[i].text.trim();
        GenreChoix.push(result);
    }
}
const addGenreTextbox=document.getElementById('selectGenre');
让GenreChoix=[]
for(var i=0;i
这是正确的解决方案:

const addGenreTextbox = document.getElementById('selectGenre');

let GenreChoix = []
for (var i = 0; i < addGenreTextbox.options.length; i++) {
    if (addGenreTextbox.options[i].selected) {
        let result = {};
        result["id"] = addGenreTextbox.options[i].value;
        result["Nom"] = addGenreTextbox.options[i].text.trim();
        GenreChoix.push(result);
    }
}
const addGenreTextbox=document.getElementById('selectGenre');
让GenreChoix=[]
for(var i=0;i
这是否回答了您的问题?如果我这样做了,我的问题是如何在JSON(const项)中插入它,无论是一种类型还是多种类型@Randy Casburn这是否回答了您的问题?如果我这样做了,我的问题是如何在JSON(const item)中插入它,无论是一种类型还是多种类型@Randy Casburn非常感谢您的帮助,我还没有考虑过这个解决方案,但现在的问题是它只让我保留最后选择的值,我将尝试找到一个解决方案并通知您我的目标是为每个选定的值生成一个新词典。我发现,我修改了您的答案,非常感谢。非常感谢您的帮助,我没有考虑过这个解决方案,但现在的问题是,它只保留了最后选定的值,我的目标是为每个选定的值生成一个新字典。我发现,我修改了你的答案,非常感谢。