JS on the Select control option options to check the sample code _javascript tips

Source: Internet
Author: User
The Javascript action Select is a common form in forms, and the following are some of the most commonly used methods of JS dynamic operation select:
Copy Code code as follows:

Create a Select dynamically
function Createselect ()
{
var myselect = document.createelement ("select");
Myselect.id = "Myselect";
Document.body.appendChild (Myselect);
}

Copy Code code as follows:

Add Options option
function AddOption ()
{
To find an object based on its ID,
var Obj=document.getelementbyid (' Myselect ');
Add an option
Obj.add (New Option ("text", "value"); This only works in IE.
Obj.options.add (New Option ("text", "value")); This compatible with IE and Firefox
}

Copy Code code as follows:

Delete all options option
function RemoveAll ()
{
var Obj=document.getelementbyid (' Myselect ');
obj.options.length=0;
}

Copy Code code as follows:

Delete an option
function Removeone ()
{
var Obj=document.getelementbyid (' Myselect ');
Index, to delete the ordinal number of the option, take the number of the currently selected option
var Index=obj.selectedindex;
Obj.options.remove (index);
}

Copy Code code as follows:

To get the text of options option
var Obj=document.getelementbyid (' Myselect ');
var Index=obj.selectedindex; Ordinal number, taking the number of the currently selected option
var val = obj.options[index].text;

Copy Code code as follows:

//Modify Options Option
Var obj=document.getelementbyid (' Myselect ');
Var index=obj.selectedindex//ordinal, taking the ordinal number of the currently selected option
var val = obj.options[index]=new option ("New text", "New value");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.