从optgroup-javascript将逗号分隔的值拆分为数组

Splitting comma separated values into array from optgroup - javascript

本文关键字:拆分 数组 分隔 optgroup-javascript      更新时间:2023-09-26

我有一些类似的代码:

var optgroup = $('#myselect').val();
var optarray = optgroup.split(',');
alert(optarray.join("'n"));

如果选择了所有三个选项,则第一行返回"option1、option2、option3"行中的内容。我希望它为每个选项返回一个单独的结果。我已经尝试了上面的代码,但我一直得到错误:

Uncaught TypeError: undefined is not a function 

我不知道这个错误是怎么回事,如果有任何帮助,我们将不胜感激。顺便说一句,当这些值被分开时,我将在数组中循环,并将每个选项存储在SQLite中表的不同行中。

如果您想获得可能的选择列表,可以使用以下代码:

var list = '';
$('#myselect option').each(function(){
  list = list + $(this).val() + ''n';
});
console.log(list);