如何在html中的select(Multiple)指定索引处插入选项?

Dip*_*pak 30 html javascript jquery

如何在元素中的指定索引处插入选项<select multiple>元素?

谢谢

Cor*_*liu 22

$("select option").eq(index).before($("<option></option>").val(val).html(text));
Run Code Online (Sandbox Code Playgroud)

  • 这要么是天才,要么是疯狂的黑客. (3认同)

Car*_*man 11

香草javascript(没有jQuery)和跨浏览器.

要在选择"mySelect"中插入新选项,值为"1",文本为"text",请在第0个现有项目之前:

mySelect.options.add(new Option("text", "1"), mySelect.options[0]);

请参阅:https://developer.mozilla.org/en/docs/Web/API/HTMLSelectElement#add%28%29