Adding options to select with javascript

后端 未结 9 994
执念已碎
执念已碎 2020-11-22 15:40

I want this javascript to create options from 12 to 100 in a select with id=\"mainSelect\", because I do not want to create all of the option tags manually. Can you give me

9条回答
  •  情话喂你
    2020-11-22 16:07

    When you create a new Option object, there are two parameters to pass: The first is the text you want to appear in the list, and the second the value to be assigned to the option.

    var myNewOption = new Option("TheText", "TheValue");
    

    You then simply assign this Option object to an empty array element, for example:

    document.theForm.theSelectObject.options[0] = myNewOption;
    

提交回复
热议问题