0

我正在尝试连接 optgroup 但选择的值。In jsFiddle when the select is simple selection it works normal. 选择São Paulo值时正确的结果:Sudeste - São Paulo 但是当我将选择更改为多个时,它会随机播放。

看看jsFiddle

4

1 回答 1

1

您的format(item)函数没有获取所选项目的父项的原因optgroup是,当函数运行时,option实际上还没有被选中。此时,select2 插件还没有option在底层select标签中将 标记为选中。

如果您 debug ,您可以在函数第一次运行时format(item)看到它是一个空数组。$('#estados').find(':selected')

相反,您需要做的是从参数中获取option元素item,然后在查询中使用它来获取optgroup.

function format(item) {
  // opt = $('#estados').find(':selected');
  // sel = opt.text();
  // og = opt.closest('optgroup').attr('label');

  var og = $(item.element[0]).closest('optgroup').attr('label');
  return og+'-'+item.text;
}
于 2016-02-12T18:23:27.407 回答