<select> tags in html How to set options that are selected by default

Source: Internet
Author: User

There are two ways of doing this.

The first is to set the selected item through the <select> property, which can control the output as needed in a dynamic language such as PHP in the background.

12345 <   Select   id     "sel"   ; <   option    value   =   "1"   >1</   option   ; <   Option    value   =   "2"    Selected   =   "selected"   >2</   Option   ; <   option    value   =   "3"   >3</   option   </   select   ;

The second is to control the selected item by the front-end JS: Compiled by www.169it.com

1234567891011 <scripttype="text/javascript">function change(){    document.getElementById("sel")[2].selected=true;}</script><selectid"sel" ><optionvalue="1">1</option><optionvalue="2">2</option><optionvalue="3">3</option></select><inputtype="button"value="修改"onclick="change()" />

Get <select> Tag The text of the selected item is JS code:

12 varval = document.all.Item.options[document.all.Item.selectedIndex].textvar  i=document.getElementById( ‘sel‘ ).options[document.getElementById( ‘sel‘).selectedIndex].value;

Some other tips for handling <select> labeling are as follows:

1) Dynamically Create Select

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

2) Add Options option

123456 functionaddOption(){//根据id查找对象,var obj=document.getElementById( ‘mySelect‘ );//添加一个选项obj.add( new  Option( "文本" , "值"));}

3) Remove all options option

1234 functionremoveAll(){var  obj=document.getElementById( ‘mySelect‘);obj.options.length=0;}

4) Delete an option

123456 functionremoveOne(){var  obj=document.getElementById( ‘mySelect‘ );//index,要删除选项的序号,这里取当前选中选项的序号varindex=obj.selectedIndex;obj.options.remove(index);}

5) Get the value of option options

123 varobj=document.getElementById( ‘mySelect‘ );var index=obj.selectedIndex;  //序号,取当前选中选项的序号varval = obj.options[index].value;

6) to get the text of options option

123 varobj=document.getElementById( ‘mySelect‘ );var index=obj.selectedIndex;  //序号,取当前选中选项的序号varval = obj.options[index].text;

7) Modify Options option

123 varobj=document.getElementById( ‘mySelect‘ );var  index=obj.selectedIndex;  //序号,取当前选中选项的序号var val = obj.options[index]= new  Option( "新文本" , "新值");

8) Delete Select

1234 functionremoveSelect(){var  mySelect = document.getElementById( "mySelect");mySelect.parentNode.removeChild(mySelect);}

<select> tags in html How to set options that are selected by default

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.