Select label sets the selected option method by default. select label

Source: Internet
Author: User

Select label sets the selected option method by default. select label

There are two methods.

FirstThe <select> attribute is used to set the selected items. This method can control the output results in dynamic languages such as php in the background as needed.

< select id = "sel" >< option value = "1" >1</ option >< option value = "2" selected = "selected" >2</ option >< option value = "3" >3</ option ></ select >

SecondTo control the selected items through front-end js:

<Script type = "text/javascript"> function change () {document. getElementById ("sel") [2]. selected = true ;} </script> <select id = "sel"> <option value = "1"> 1 </option> <option value = "2"> 2 </option> <option value = "3"> 3 </option> </select> <input type = "button" value = "modify" onclick = "change () "/>

The js Code for getting the <select> label selected item text is:

var val = document.all.Item.options[document.all.Item.selectedIndex].textvar i=document.getElementById( 'sel' ).options[document.getElementById( 'sel' ).selectedIndex].value;

Tips for some other operations on the <select> label are as follows:

1) dynamically create select

function createSelect(){var mySelect = document.createElement( "select" );mySelect.id = "mySelect" ;document.body.appendChild(mySelect);}

2) Add option

Function addOption () {// find the object by id, var obj = document. getElementById ('myselect'); // Add an option obj. add (new Option ("text", "value "));}

3) delete all option options

function removeAll(){var obj=document.getElementById( 'mySelect' );obj.options.length=0;}

4) delete an option

Function removeOne () {var obj = document. getElementById ('myselect'); // index. The sequence number of the option to be deleted. The number var index = obj of the selected option is used here. selectedIndex; obj. options. remove (index );}

5) obtain the option value

Var obj = document. getElementById ('myselect'); var index = obj. selectedIndex; // The serial number. The serial number var val = obj. options [index]. value;

6) obtain the option text

Var obj = document. getElementById ('myselect'); var index = obj. selectedIndex; // The serial number. The serial number var val = obj. options [index]. text;

7) Modify option

Var obj = document. getElementById ('myselect'); var index = obj. selectedIndex; // The serial number. The serial number var val = obj. options [index] = new Option ("new text", "new Value ");

8) delete select

function removeSelect(){var mySelect = document.getElementById( "mySelect" );mySelect.parentNode.removeChild(mySelect);}

The default Selection Method for the preceding select tag settings is all the content shared by the editor. I hope you can give us a reference and support the help house.

Related Article

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.