我从csv加载数据并将其拆分为行和单元格。在我的csv文件中,每行的第一个单元格应该是我的选择组,其余的应该是当前选择组中的选项。它只适用于选项组(一个循环(,但是当我添加选项时,我在选项组文本中获得了额外的双引号并且没有显示。请帮忙
//loading file
$.get("file.csv",function(data)
//spliting data
{
var rows=data.split("n");
for (var i=0; i<rows.length; i++)
{
var cells=rows[i].split(";");
//first cell in the row = optgroup
$('select').append("<optgroup>"+cells[0]);
//second loop - the rest of cells in the row = options
for (var j=1; j<cells.length; j++)
{
$('select optgroup').eq(i).append('<option>'+cells[j])
}
}
});
您需要正确关闭标签,因为您是以这种方式添加元素的。此外,optgroup
没有文本内容。将其添加到标签。
//loading file
$.get("file.csv", function(data)
//spliting data
{
var rows = data.split("n");
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].split(";");
//first cell in the row = optgroup
$('select').append("<optgroup label='" + cells[0] + "'></optgroup>");
//second loop - the rest of cells in the row = options
for (var j = 1; j < cells.length; j++) {
$('select optgroup').eq(i).append('<option>' + cells[j] + '</option>')
}
}
});