Javascript 填充两个选择框';有一系列选项吗?

Javascript 填充两个选择框';有一系列选项吗?,javascript,jquery,html,Javascript,Jquery,Html,我有一个数组,我想用来填充两个不同的选择框,但我看到一些奇怪的行为。填充第一个选择可以正常工作,但是当将相同的列表添加到第二个选择框时,第一个选择将被清除,最后一个项目将在第二个选择框中被选中 var optionList = [] optionList.push(new Option("waba", "waba")) optionList.push(new Option("shaba", "shaba")) $('#first_select').html(optionList); //wo

我有一个数组,我想用来填充两个不同的选择框,但我看到一些奇怪的行为。填充第一个选择可以正常工作,但是当将相同的列表添加到第二个选择框时,第一个选择将被清除,最后一个项目将在第二个选择框中被选中

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

您必须使用
jQuery.clone来克隆它们,如下所示:

$('#first').html(optionList); // append the original options
$('#second').html($(optionList).clone()); // clone them and append the cloned ones

我想你需要克隆这个选项列表

var optionList = []

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

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

在一条语句中设置这两条语句的html内容:

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

jsfiddle:

我喜欢这个答案,但在我的情况下不容易。