使用jQuery向下拉列表中添加选项的最简单方法是什么?

这行吗?

$("#mySelect").append('<option value=1>My option</option>');

当前回答

$('#select_id').append($('<option>',{ value: v, text: t }));

其他回答

Try

mySelect.innerHTML+= '<option value=1>My option</option>';

btn.onclick=_=>mySelect.innerHTML+=`<optionselected>${+newDate}</option>`<button id=“btn”>添加选项</button><select id=“mySelect”></select>

您可以尝试以下代码附加到选项

  <select id="mySelect"></select>

    <script>
          $("#mySelect").append($("<option></option>").val("1").html("My enter code hereoption"));
    </script>

如果您在select中有optgroup,则在DOM中出现错误。

我认为最好的方法是:

$("#select option:last").after($('<option value="1">my option</option>'));

如果要在选择中的特定索引处插入新选项:

$("#my_select option").eq(2).before($('<option>', {
    value: 'New Item',
    text: 'New Item'
}));

这将在选择中插入“新项目”作为第三个项目。

您可以使用以下语法添加选项,也可以访问jQuery中的way handle选项以了解更多详细信息。

$('#select').append($('<option>',{value:1,text:'One'}));$('#select').append('<option value=“1”>一个</option>');var option=新选项(文本,值)$('#select').append($(选项));