我有一个数组,我想用它来填充两个不同的选择框,但我看到了一些奇怪的行为。填充第一个选择工作正常但是当将相同的列表添加到第二个选择框时,第一个选择被清除并且在第二个选择框中选择最后一个项目。
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
最佳答案
在一条语句中设置两者的 html 内容:
$('#first, #second').html(optionList);
jsfiddle:https://jsfiddle.net/tcuow32f/1/
关于javascript - 用一组选项填充两个选择框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42169306/