Home >Web Front-end >JS Tutorial >How to set the default selected option based on the select tag?
Below I will share with you an article on how to set the default selected option for the select tag. It has a good reference value and I hope it will be helpful to everyone.
There are two methods.
##The first sets the selected item through the attribute of 221f08282418e2996498697df914ce4e. This method can be used in dynamic languages such as php in the background according to Need to control the output results.
< select id = "sel" > < option value = "1" >1</ option > < option value = "2" selected = "selected" >2</ option > < option value = "3" >3</ option > </ select >
The second type is to control the selected item 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 = "修改" onclick = "change()" />The js code to get the text of the selected item in the 221f08282418e2996498697df914ce4e tag is:
var val = document.all.Item.options[document.all.Item.selectedIndex].text var i=document.getElementById( 'sel' ).options[document.getElementById( 'sel' ).selectedIndex].value;
Some other techniques for operating the 221f08282418e2996498697df914ce4e tag are as follows:
1) Dynamically create select
function createSelect(){ var mySelect = document.createElement( "select" ); mySelect.id = "mySelect" ; document.body.appendChild(mySelect); }
2) Add option option
function addOption(){ //根据id查找对象, var obj=document.getElementById( 'mySelect' ); //添加一个选项 obj.add( new Option( "文本" , "值" )); }
3) Delete all options option
function removeAll(){ var obj=document.getElementById( 'mySelect' ); obj.options.length=0; }
4) Delete an option option
function removeOne(){ var obj=document.getElementById( 'mySelect' ); //index,要删除选项的序号,这里取当前选中选项的序号 var index=obj.selectedIndex; obj.options.remove(index); }
5) Get the value of option
var obj=document.getElementById( 'mySelect' ); var index=obj.selectedIndex; //序号,取当前选中选项的序号 var val = obj.options[index].value;
6) Get the text of option
var obj=document.getElementById( 'mySelect' ); var index=obj.selectedIndex; //序号,取当前选中选项的序号 var val = obj.options[index].text;
7) Modify option option
var obj=document.getElementById( 'mySelect' ); var index=obj.selectedIndex; //序号,取当前选中选项的序号 var val = obj.options[index]= new Option( "新文本" , "新值" );
8) Delete select
function removeSelect(){ var mySelect = document.getElementById( "mySelect" ); mySelect.parentNode.removeChild(mySelect); }The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. Related articles:
Sending a post request through axios found that springMVC could not receive parameters (detailed tutorial)
Passed Vue implements adding axios components to solve the problem of null parameters in post (detailed tutorial)
In vue, use axios to handle the problem of post request parameters (detailed tutorial)
The above is the detailed content of How to set the default selected option based on the select tag?. For more information, please follow other related articles on the PHP Chinese website!