使用 jquery 将项目添加到列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3854120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:13:50  来源:igfitidea点击:

add item to a listbox using jquery

jquery

提问by andrew Sullivan

How to add item to a listBox using jquery.

如何使用 jquery 将项目添加到列表框。

for example in the following listbox

例如在下面的列表框中

<option value="1"></option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="0">All</option>

回答by Reigel

$('#Select1').append('<option value="5">item 5</option>'); // adds item 5 at the end
$('#Select1 option').eq(0).after('<option value="new">new</option>'); // add new at the second position.

回答by Joyce Babu

Several methods are there. You can use

有几种方法。您可以使用

$('<option value="5">item 5</option>').appendTo('#listbox');

$('#listbox').append('<option value="5">item 5</option>');