![](/img/trans.png)
[英]How to remove all options from a dropdown using jQuery / JavaScript
[英]How to set options of a dropdown using Javascript/jQuery?
有誰知道如何使用javascript或jquery設置下拉菜單的選項和值? 我正在使用這個HTML:
<select size="1" id="D1">
</select>
謝謝您的幫助。
另一種方法是使用select
的add方法 :
var select = $("#select")[0];
select.add(new Option("one", 1));
select.add(new Option("two", 2));
select.add(new Option("three", 3));
示例: http : //jsfiddle.net/pc9Dz/
或者另一種方式,通過直接為select
的options
集合分配值:
var select = $("#select")[0];
select.options[0] = new Option("one", 1);
select.options[1] = new Option("two", 2);
這是通過使用options數組使用javascript(類似於Andrew Whitaker的答案)刪除選項的另一種方法:
var select = document.getElementById("D1");
select.options.length = 0;//remove all options
有幾種不同的方式。 一個是:
$("#D1").append("<option>Fred</option>");
$("#dropdown").html("<option value='val1'>label 1</option><option value='val2' selected='selected'>label 2</option>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.