我在 jQuery 中将项目添加到列表时遇到了麻烦,这是我遵循的语法。两者都不起作用。
$("myList")[0].options.add(new Option("ListText", value)); //does not work
$("myList").append($('<option>', {
text: "ListText",
value: value
})); //does not work
$("myList").append(new Option("ListText", value)); //does not work.
这是我的代码的外观
<select id="myList" class="DropDownList Test" name="List">
<option value="selectid" selected="selected">--Please Select--</option>
<option value="test1">a</option>
<option value="test2">b</option>
<option value="test3">c</option>
</select>
让成像我做这样的事情,如果我也这样做它失败了。
function updateTheList(ListID, value, position) {
switch (position) {
case '1':
$(ListID).append(new Option("Text", value));
break;
} //what is wrong with this syntax
}
if ($(Name+ "-ListID").is(':visible')) {
updateTheList($(Name+ "-ListID"), value, position);
} // it does not work
请告诉我正确的方法。
谢谢