javascript - 用一组选项填充两个选择框?

标签 javascript jquery html

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

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

最佳答案

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

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

jsfiddle:https://jsfiddle.net/tcuow32f/1/

关于javascript - 用一组选项填充两个选择框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42169306/

相关文章:

javascript - 如何计算滚动条的宽度?

c# - 如何通过 jQuery/javascript 在 ASP.NET 转发器中选择特定的 html 元素?

html - Bootstrap : FIxed div height of parent element

java - 使用 for-each 循环遍历页面时的 Selenium 陈旧元素

javascript - 有没有JS函数可以求小数点前后的值

javascript - 插入nodejs后MySql更新

jquery - 我如何在phonegap中动态导航到另一个页面

Javascript/Jquery委托(delegate)点击触发视频功能两次

html - 页面加载后移动 View 变小

php - 如何在链接单击上插入和删除 div 及其内容