Nar*_*uto 1 html jquery asp-classic drop-down-menu
我在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.
Run Code Online (Sandbox Code Playgroud)
这是我的代码看起来如何
<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>
Run Code Online (Sandbox Code Playgroud)
让成像我做这样的事情,如果我也这样做也失败了.
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
Run Code Online (Sandbox Code Playgroud)
请告诉我正确的方法.
谢谢
您缺少#
jquery选择器上的哈希.用这个
$("#myList").append($('<option>', {
text: "ListText",
value: value
}));
Run Code Online (Sandbox Code Playgroud)
要插入特定位置,请使用此选项.上面你做错的主要是将Jquery对象传递给函数,然后将它包装在另一个jquery对象中.
function updateTheList(listId, text, value, position) {
$(listId + ' option:eq(' + position + ')')
.after('<option value=\"' + value + '\">' + text + '</option>');
}
if ($('#SomeListId').is(':visible')) {
updateTheList('#SomeListId', 'Some text', 'Some value', 2);
}
Run Code Online (Sandbox Code Playgroud)
同时尝试坚持命名约定.使用camelCase作为方法和属性,使用PascalCase作为对象.
归档时间: |
|
查看次数: |
3196 次 |
最近记录: |