我有一个数组,想用来填充两个不同的选择框,但是我看到一些奇怪的行为。填充第一个选择的效果很好,但是将相同列表添加到第二个选择框时,将清除第一个选择,并在第二个选择框上选择最后一个项目。

var optionList = []

optionList.push(new Option("waba", "waba"))
optionList.push(new Option("shaba", "shaba"))

$('#first_select').html(optionList); //works fine
$('#second_select').html(optionList); //clears first select and last item is selected

JS Fiddle Example

最佳答案

我认为您需要克隆optionList

var optionList = []

optionList.push(new Option("waba", "waba"))
optionList.push(new Option("shaba", "shaba"))

$('#first').html(optionList);
$('#second').html($(optionList).clone());

09-07 13:08